xref: /plan9/sys/src/cmd/fossil/check.c (revision 4dc626cd82534e522e4a6a32aec8b7d6f806e477)
1 #include "stdinc.h"
2 #include "dat.h"
3 #include "fns.h"
4 
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*, ...);
18 
19 #pragma varargck argpos error 2
20 #pragma varargck argpos warn 2
21 
22 static Fsck*
23 checkInit(Fsck *chk)
24 {
25 	chk->cache = chk->fs->cache;
26 	chk->nblocks = cacheLocalSize(chk->cache, PartData);;
27 	chk->bsize = chk->fs->blockSize;
28 	chk->walkdepth = 0;
29 	chk->hint = 0;
30 	chk->quantum = chk->nblocks/100;
31 	if(chk->quantum == 0)
32 		chk->quantum = 1;
33 	if(chk->print == nil)
34 		chk->print = printnop;
35 	if(chk->clre == nil)
36 		chk->clre = clrenop;
37 	if(chk->close == nil)
38 		chk->close = closenop;
39 	if(chk->clri == nil)
40 		chk->clri = clrinop;
41 	return chk;
42 }
43 
44 /*
45  * BUG: Should merge checkEpochs and checkDirs so that
46  * bad blocks are only reported once, and so that errors in checkEpochs
47  * can have the affected file names attached, and so that the file system
48  * is only read once.
49  *
50  * Also should summarize the errors instead of printing for every one
51  * (e.g., XXX bad or unreachable blocks in /active/usr/rsc/foo).
52  */
53 
54 void
55 fsCheck(Fsck *chk)
56 {
57 	Block *b;
58 	Super super;
59 
60 	checkInit(chk);
61 	b = superGet(chk->cache, &super);
62 	if(b == nil){
63 		chk->print("could not load super block: %R");
64 		return;
65 	}
66 	blockPut(b);
67 
68 	chk->hint = super.active;
69 	checkEpochs(chk);
70 
71 	chk->smap = vtMemAllocZ(chk->nblocks/8+1);
72 	checkDirs(chk);
73 	vtMemFree(chk->smap);
74 }
75 
76 static void checkEpoch(Fsck*, u32int);
77 
78 /*
79  * Walk through all the blocks in the write buffer.
80  * Then we can look for ones we missed -- those are leaks.
81  */
82 static void
83 checkEpochs(Fsck *chk)
84 {
85 	u32int e;
86 	uint nb;
87 
88 	nb = chk->nblocks;
89 	chk->amap = vtMemAllocZ(nb/8+1);
90 	chk->emap = vtMemAllocZ(nb/8+1);
91 	chk->xmap = vtMemAllocZ(nb/8+1);
92 	chk->errmap = vtMemAllocZ(nb/8+1);
93 
94 	for(e = chk->fs->ehi; e >= chk->fs->elo; e--){
95 		memset(chk->emap, 0, chk->nblocks/8+1);
96 		memset(chk->xmap, 0, chk->nblocks/8+1);
97 		checkEpoch(chk, e);
98 	}
99 	checkLeak(chk);
100 	vtMemFree(chk->amap);
101 	vtMemFree(chk->emap);
102 	vtMemFree(chk->xmap);
103 	vtMemFree(chk->errmap);
104 }
105 
106 static void
107 checkEpoch(Fsck *chk, u32int epoch)
108 {
109 	u32int a;
110 	Block *b;
111 	Entry e;
112 	Label l;
113 
114 	chk->print("checking epoch %ud...\n", epoch);
115 
116 	for(a=0; a<chk->nblocks; a++){
117 		if(!readLabel(chk->cache, &l, (a+chk->hint)%chk->nblocks)){
118 			error(chk, "could not read label for addr %.8#ux", a);
119 			continue;
120 		}
121 		if(l.tag == RootTag && l.epoch == epoch)
122 			break;
123 	}
124 
125 	if(a == chk->nblocks){
126 		chk->print("could not find root block for epoch %ud", epoch);
127 		return;
128 	}
129 
130 	a = (a+chk->hint)%chk->nblocks;
131 	b = cacheLocalData(chk->cache, a, BtDir, RootTag, OReadOnly, 0);
132 	if(b == nil){
133 		error(chk, "could not read root block %.8#ux: %R", a);
134 		return;
135 	}
136 
137 	/* no one should point at root blocks */
138 	setBit(chk->amap, a);
139 	setBit(chk->emap, a);
140 	setBit(chk->xmap, a);
141 
142 	/*
143 	 * First entry is the rest of the file system.
144 	 * Second entry is link to previous epoch root,
145 	 * just a convenience to help the search.
146 	 */
147 	if(!entryUnpack(&e, b->data, 0)){
148 		error(chk, "could not unpack root block %.8#ux: %R", a);
149 		blockPut(b);
150 		return;
151 	}
152 	walkEpoch(chk, b, e.score, BtDir, e.tag, epoch);
153 	if(entryUnpack(&e, b->data, 1))
154 		chk->hint = globalToLocal(e.score);
155 	blockPut(b);
156 }
157 
158 /*
159  * When b points at bb, need to check:
160  *
161  * (i) b.e in [bb.e, bb.eClose)
162  * (ii) if b.e==bb.e,  then no other b' in e points at bb.
163  * (iii) if !(b.state&Copied) and b.e==bb.e then no other b' points at bb.
164  * (iv) if b is active then no other active b' points at bb.
165  * (v) if b is a past life of b' then only one of b and b' is active
166  *	(too hard to check)
167  */
168 static int
169 walkEpoch(Fsck *chk, Block *b, uchar score[VtScoreSize], int type, u32int tag,
170 	u32int epoch)
171 {
172 	int i, ret;
173 	u32int addr, ep;
174 	Block *bb;
175 	Entry e;
176 
177 	if(b && chk->walkdepth == 0 && chk->printblocks)
178 		chk->print("%V %d %#.8ux %#.8ux\n", b->score, b->l.type,
179 			b->l.tag, b->l.epoch);
180 
181 	if(!chk->useventi && globalToLocal(score) == NilBlock)
182 		return 1;
183 
184 	chk->walkdepth++;
185 
186 	bb = cacheGlobal(chk->cache, score, type, tag, OReadOnly);
187 	if(bb == nil){
188 		error(chk, "could not load block %V type %d tag %ux: %R",
189 			score, type, tag);
190 		chk->walkdepth--;
191 		return 0;
192 	}
193 	if(chk->printblocks)
194 		chk->print("%*s%V %d %#.8ux %#.8ux\n", chk->walkdepth*2, "",
195 			score, type, tag, bb->l.epoch);
196 
197 	ret = 0;
198 	addr = globalToLocal(score);
199 	if(addr == NilBlock){
200 		ret = 1;
201 		goto Exit;
202 	}
203 
204 	if(b){
205 		/* (i) */
206 		if(b->l.epoch < bb->l.epoch || bb->l.epochClose <= b->l.epoch){
207 			error(chk, "walk: block %#ux [%ud, %ud) points at %#ux [%ud, %ud)",
208 				b->addr, b->l.epoch, b->l.epochClose,
209 				bb->addr, bb->l.epoch, bb->l.epochClose);
210 			goto Exit;
211 		}
212 
213 		/* (ii) */
214 		if(b->l.epoch == epoch && bb->l.epoch == epoch){
215 			if(getBit(chk->emap, addr)){
216 				error(chk, "walk: epoch join detected: addr %#ux %L",
217 					bb->addr, &bb->l);
218 				goto Exit;
219 			}
220 			setBit(chk->emap, addr);
221 		}
222 
223 		/* (iii) */
224 		if(!(b->l.state&BsCopied) && b->l.epoch == bb->l.epoch){
225 			if(getBit(chk->xmap, addr)){
226 				error(chk, "walk: copy join detected; addr %#ux %L",
227 					bb->addr, &bb->l);
228 				goto Exit;
229 			}
230 			setBit(chk->xmap, addr);
231 		}
232 	}
233 
234 	/* (iv) */
235 	if(epoch == chk->fs->ehi){
236 		/*
237 		 * since epoch==fs->ehi is first, amap is same as
238 		 * ``have seen active''
239 		 */
240 		if(getBit(chk->amap, addr)){
241 			error(chk, "walk: active join detected: addr %#ux %L",
242 				bb->addr, &bb->l);
243 			goto Exit;
244 		}
245 		if(bb->l.state&BsClosed)
246 			error(chk, "walk: addr %#ux: block is in active tree but is closed",
247 				addr);
248 	}else
249 		if(!getBit(chk->amap, addr))
250 			if(!(bb->l.state&BsClosed)){
251 				// error(chk, "walk: addr %#ux: block is not in active tree, not closed (%d)",
252 				// addr, bb->l.epochClose);
253 				chk->close(chk, bb, epoch+1);
254 				chk->nclose++;
255 			}
256 
257 	if(getBit(chk->amap, addr)){
258 		ret = 1;
259 		goto Exit;
260 	}
261 	setBit(chk->amap, addr);
262 
263 	if(chk->nseen++%chk->quantum == 0)
264 		chk->print("check: visited %d/%d blocks (%.0f%%)\n",
265 			chk->nseen, chk->nblocks, chk->nseen*100./chk->nblocks);
266 
267 	b = nil;		/* make sure no more refs to parent */
268 	USED(b);
269 
270 	switch(type){
271 	default:
272 		/* pointer block */
273 		for(i = 0; i < chk->bsize/VtScoreSize; i++)
274 			if(!walkEpoch(chk, bb, bb->data + i*VtScoreSize,
275 			    type-1, tag, epoch)){
276 				setBit(chk->errmap, bb->addr);
277 				chk->clrp(chk, bb, i);
278 				chk->nclrp++;
279 			}
280 		break;
281 	case BtData:
282 		break;
283 	case BtDir:
284 		for(i = 0; i < chk->bsize/VtEntrySize; i++){
285 			if(!entryUnpack(&e, bb->data, i)){
286 				// error(chk, "walk: could not unpack entry: %ux[%d]: %R",
287 				//	addr, i);
288 				setBit(chk->errmap, bb->addr);
289 				chk->clre(chk, bb, i);
290 				chk->nclre++;
291 				continue;
292 			}
293 			if(!(e.flags & VtEntryActive))
294 				continue;
295 if(0)			fprint(2, "%x[%d] tag=%x snap=%d score=%V\n",
296 				addr, i, e.tag, e.snap, e.score);
297 			ep = epoch;
298 			if(e.snap != 0){
299 				if(e.snap >= epoch){
300 					// error(chk, "bad snap in entry: %ux[%d] snap = %ud: epoch = %ud",
301 					//	addr, i, e.snap, epoch);
302 					setBit(chk->errmap, bb->addr);
303 					chk->clre(chk, bb, i);
304 					chk->nclre++;
305 					continue;
306 				}
307 				continue;
308 			}
309 			if(e.flags & VtEntryLocal){
310 				if(e.tag < UserTag)
311 				if(e.tag != RootTag || tag != RootTag || i != 1){
312 					// error(chk, "bad tag in entry: %ux[%d] tag = %ux",
313 					//	addr, i, e.tag);
314 					setBit(chk->errmap, bb->addr);
315 					chk->clre(chk, bb, i);
316 					chk->nclre++;
317 					continue;
318 				}
319 			}else
320 				if(e.tag != 0){
321 					// error(chk, "bad tag in entry: %ux[%d] tag = %ux",
322 					//	addr, i, e.tag);
323 					setBit(chk->errmap, bb->addr);
324 					chk->clre(chk, bb, i);
325 					chk->nclre++;
326 					continue;
327 				}
328 			if(!walkEpoch(chk, bb, e.score, entryType(&e),
329 			    e.tag, ep)){
330 				setBit(chk->errmap, bb->addr);
331 				chk->clre(chk, bb, i);
332 				chk->nclre++;
333 			}
334 		}
335 		break;
336 	}
337 
338 	ret = 1;
339 
340 Exit:
341 	chk->walkdepth--;
342 	blockPut(bb);
343 	return ret;
344 }
345 
346 /*
347  * We've just walked the whole write buffer.  Notice blocks that
348  * aren't marked available but that we didn't visit.  They are lost.
349  */
350 static void
351 checkLeak(Fsck *chk)
352 {
353 	u32int a, nfree, nlost;
354 	Block *b;
355 	Label l;
356 
357 	nfree = 0;
358 	nlost = 0;
359 
360 	for(a = 0; a < chk->nblocks; a++){
361 		if(!readLabel(chk->cache, &l, a)){
362 			error(chk, "could not read label: addr %ux %d %d: %R",
363 				a, l.type, l.state);
364 			continue;
365 		}
366 		if(getBit(chk->amap, a))
367 			continue;
368 		if(l.state == BsFree || l.epochClose <= chk->fs->elo ||
369 		    l.epochClose == l.epoch){
370 			nfree++;
371 			setBit(chk->amap, a);
372 			continue;
373 		}
374 		if(l.state&BsClosed)
375 			continue;
376 		nlost++;
377 		// warn(chk, "unreachable block: addr %ux type %d tag %ux state %s epoch %ud close %ud",
378 		// 	a, l.type, l.tag, bsStr(l.state), l.epoch, l.epochClose);
379 		b = cacheLocal(chk->cache, PartData, a, OReadOnly);
380 		if(b == nil){
381 			error(chk, "could not read block %#.8ux", a);
382 			continue;
383 		}
384 		chk->close(chk, b, 0);
385 		chk->nclose++;
386 		setBit(chk->amap, a);
387 		blockPut(b);
388 	}
389 	chk->print("fsys blocks: total=%ud used=%ud(%.1f%%) free=%ud(%.1f%%) lost=%ud(%.1f%%)\n",
390 		chk->nblocks,
391 		chk->nblocks-nfree-nlost,
392 		100.*(chk->nblocks - nfree - nlost)/chk->nblocks,
393 		nfree, 100.*nfree/chk->nblocks,
394 		nlost, 100.*nlost/chk->nblocks);
395 }
396 
397 
398 /*
399  * Check that all sources in the tree are accessible.
400  */
401 static Source *
402 openSource(Fsck *chk, Source *s, char *name, uchar *bm, u32int offset,
403 	u32int gen, int dir, MetaBlock *mb, int i, Block *b)
404 {
405 	Source *r;
406 
407 	r = nil;
408 	if(getBit(bm, offset)){
409 		warn(chk, "multiple references to source: %s -> %d",
410 			name, offset);
411 		goto Err;
412 	}
413 	setBit(bm, offset);
414 
415 	r = sourceOpen(s, offset, OReadOnly, 0);
416 	if(r == nil){
417 		warn(chk, "could not open source: %s -> %d: %R", name, offset);
418 		goto Err;
419 	}
420 
421 	if(r->gen != gen){
422 		warn(chk, "source has been removed: %s -> %d", name, offset);
423 		goto Err;
424 	}
425 
426 	if(r->dir != dir){
427 		warn(chk, "dir mismatch: %s -> %d", name, offset);
428 		goto Err;
429 	}
430 	return r;
431 Err:
432 	chk->clri(chk, name, mb, i, b);
433 	chk->nclri++;
434 	if(r)
435 		sourceClose(r);
436 	return nil;
437 }
438 
439 typedef struct MetaChunk MetaChunk;
440 struct MetaChunk {
441 	ushort	offset;
442 	ushort	size;
443 	ushort	index;
444 };
445 
446 static int
447 offsetCmp(void *s0, void *s1)
448 {
449 	MetaChunk *mc0, *mc1;
450 
451 	mc0 = s0;
452 	mc1 = s1;
453 	if(mc0->offset < mc1->offset)
454 		return -1;
455 	if(mc0->offset > mc1->offset)
456 		return 1;
457 	return 0;
458 }
459 
460 /*
461  * Fsck that MetaBlock has reasonable header, sorted entries,
462  */
463 static int
464 chkMetaBlock(MetaBlock *mb)
465 {
466 	MetaChunk *mc;
467 	int oo, o, n, i;
468 	uchar *p;
469 
470 	mc = vtMemAlloc(mb->nindex*sizeof(MetaChunk));
471 	p = mb->buf + MetaHeaderSize;
472 	for(i = 0; i < mb->nindex; i++){
473 		mc[i].offset = p[0]<<8 | p[1];
474 		mc[i].size =   p[2]<<8 | p[3];
475 		mc[i].index = i;
476 		p += MetaIndexSize;
477 	}
478 
479 	qsort(mc, mb->nindex, sizeof(MetaChunk), offsetCmp);
480 
481 	/* check block looks ok */
482 	oo = MetaHeaderSize + mb->maxindex*MetaIndexSize;
483 	o = oo;
484 	n = 0;
485 	for(i = 0; i < mb->nindex; i++){
486 		o = mc[i].offset;
487 		n = mc[i].size;
488 		if(o < oo)
489 			goto Err;
490 		oo += n;
491 	}
492 	if(o+n > mb->size || mb->size - oo != mb->free)
493 		goto Err;
494 
495 	vtMemFree(mc);
496 	return 1;
497 
498 Err:
499 if(0){
500 	fprint(2, "metaChunks failed!\n");
501 	oo = MetaHeaderSize + mb->maxindex*MetaIndexSize;
502 	for(i=0; i<mb->nindex; i++){
503 		fprint(2, "\t%d: %d %d\n", i, mc[i].offset,
504 			mc[i].offset + mc[i].size);
505 		oo += mc[i].size;
506 	}
507 	fprint(2, "\tused=%d size=%d free=%d free2=%d\n",
508 		oo, mb->size, mb->free, mb->size - oo);
509 }
510 	vtMemFree(mc);
511 	return 0;
512 }
513 
514 static void
515 scanSource(Fsck *chk, char *name, Source *r)
516 {
517 	u32int a, nb, o;
518 	Block *b;
519 	Entry e;
520 
521 	if(!chk->useventi && globalToLocal(r->score)==NilBlock)
522 		return;
523 	if(!sourceGetEntry(r, &e)){
524 		error(chk, "could not get entry for %s", name);
525 		return;
526 	}
527 	a = globalToLocal(e.score);
528 	if(!chk->useventi && a==NilBlock)
529 		return;
530 	if(getBit(chk->smap, a))
531 		return;
532 	setBit(chk->smap, a);
533 
534 	nb = (sourceGetSize(r) + r->dsize-1) / r->dsize;
535 	for(o = 0; o < nb; o++){
536 		b = sourceBlock(r, o, OReadOnly);
537 		if(b == nil){
538 			error(chk, "could not read block in data file %s", name);
539 			continue;
540 		}
541 		if(b->addr != NilBlock && getBit(chk->errmap, b->addr)){
542 			warn(chk, "previously reported error in block %ux is in file %s",
543 				b->addr, name);
544 		}
545 		blockPut(b);
546 	}
547 }
548 
549 /*
550  * Walk the source tree making sure that the BtData
551  * sources containing directory entries are okay.
552  */
553 static void
554 chkDir(Fsck *chk, char *name, Source *source, Source *meta)
555 {
556 	int i;
557 	u32int a1, a2, nb, o;
558 	char *s, *nn;
559 	uchar *bm;
560 	Block *b, *bb;
561 	DirEntry de;
562 	Entry e1, e2;
563 	MetaBlock mb;
564 	MetaEntry me;
565 	Source *r, *mr;
566 
567 	if(!chk->useventi && globalToLocal(source->score)==NilBlock &&
568 	    globalToLocal(meta->score)==NilBlock)
569 		return;
570 
571 	if(!sourceLock2(source, meta, OReadOnly)){
572 		warn(chk, "could not lock sources for %s: %R", name);
573 		return;
574 	}
575 	if(!sourceGetEntry(source, &e1) || !sourceGetEntry(meta, &e2)){
576 		warn(chk, "could not load entries for %s: %R", name);
577 		return;
578 	}
579 	a1 = globalToLocal(e1.score);
580 	a2 = globalToLocal(e2.score);
581 	if((!chk->useventi && a1==NilBlock && a2==NilBlock)
582 	|| (getBit(chk->smap, a1) && getBit(chk->smap, a2))){
583 		sourceUnlock(source);
584 		sourceUnlock(meta);
585 		return;
586 	}
587 	setBit(chk->smap, a1);
588 	setBit(chk->smap, a2);
589 
590 	bm = vtMemAllocZ(sourceGetDirSize(source)/8 + 1);
591 
592 	nb = (sourceGetSize(meta) + meta->dsize - 1)/meta->dsize;
593 	for(o = 0; o < nb; o++){
594 		b = sourceBlock(meta, o, OReadOnly);
595 		if(b == nil){
596 			error(chk, "could not read block in meta file: %s[%ud]: %R",
597 				name, o);
598 			continue;
599 		}
600 if(0)		fprint(2, "source %V:%d block %d addr %d\n", source->score,
601 			source->offset, o, b->addr);
602 		if(b->addr != NilBlock && getBit(chk->errmap, b->addr))
603 			warn(chk, "previously reported error in block %ux is in %s",
604 				b->addr, name);
605 
606 		if(!mbUnpack(&mb, b->data, meta->dsize)){
607 			error(chk, "could not unpack meta block: %s[%ud]: %R",
608 				name, o);
609 			blockPut(b);
610 			continue;
611 		}
612 		if(!chkMetaBlock(&mb)){
613 			error(chk, "bad meta block: %s[%ud]: %R", name, o);
614 			blockPut(b);
615 			continue;
616 		}
617 		s = nil;
618 		for(i=mb.nindex-1; i>=0; i--){
619 			meUnpack(&me, &mb, i);
620 			if(!deUnpack(&de, &me)){
621 				error(chk,
622 				  "could not unpack dir entry: %s[%ud][%d]: %R",
623 					name, o, i);
624 				continue;
625 			}
626 			if(s && strcmp(s, de.elem) <= 0)
627 				error(chk,
628 			   "dir entry out of order: %s[%ud][%d] = %s last = %s",
629 					name, o, i, de.elem, s);
630 			vtMemFree(s);
631 			s = vtStrDup(de.elem);
632 			nn = smprint("%s/%s", name, de.elem);
633 			if(nn == nil){
634 				error(chk, "out of memory");
635 				continue;
636 			}
637 			if(chk->printdirs)
638 				if(de.mode&ModeDir)
639 					chk->print("%s/\n", nn);
640 			if(chk->printfiles)
641 				if(!(de.mode&ModeDir))
642 					chk->print("%s\n", nn);
643 			if(!(de.mode & ModeDir)){
644 				r = openSource(chk, source, nn, bm, de.entry,
645 					de.gen, 0, &mb, i, b);
646 				if(r != nil){
647 					if(sourceLock(r, OReadOnly)){
648 						scanSource(chk, nn, r);
649 						sourceUnlock(r);
650 					}
651 					sourceClose(r);
652 				}
653 				deCleanup(&de);
654 				free(nn);
655 				continue;
656 			}
657 
658 			r = openSource(chk, source, nn, bm, de.entry,
659 				de.gen, 1, &mb, i, b);
660 			if(r == nil){
661 				deCleanup(&de);
662 				free(nn);
663 				continue;
664 			}
665 
666 			mr = openSource(chk, source, nn, bm, de.mentry,
667 				de.mgen, 0, &mb, i, b);
668 			if(mr == nil){
669 				sourceClose(r);
670 				deCleanup(&de);
671 				free(nn);
672 				continue;
673 			}
674 
675 			if(!(de.mode&ModeSnapshot) || chk->walksnapshots)
676 				chkDir(chk, nn, r, mr);
677 
678 			sourceClose(mr);
679 			sourceClose(r);
680 			deCleanup(&de);
681 			free(nn);
682 			deCleanup(&de);
683 
684 		}
685 		vtMemFree(s);
686 		blockPut(b);
687 	}
688 
689 	nb = sourceGetDirSize(source);
690 	for(o=0; o<nb; o++){
691 		if(getBit(bm, o))
692 			continue;
693 		r = sourceOpen(source, o, OReadOnly, 0);
694 		if(r == nil)
695 			continue;
696 		warn(chk, "non referenced entry in source %s[%d]", name, o);
697 		if((bb = sourceBlock(source, o/(source->dsize/VtEntrySize),
698 		    OReadOnly)) != nil){
699 			if(bb->addr != NilBlock){
700 				setBit(chk->errmap, bb->addr);
701 				chk->clre(chk, bb, o%(source->dsize/VtEntrySize));
702 				chk->nclre++;
703 			}
704 			blockPut(bb);
705 		}
706 		sourceClose(r);
707 	}
708 
709 	sourceUnlock(source);
710 	sourceUnlock(meta);
711 	vtMemFree(bm);
712 }
713 
714 static void
715 checkDirs(Fsck *chk)
716 {
717 	Source *r, *mr;
718 
719 	sourceLock(chk->fs->source, OReadOnly);
720 	r = sourceOpen(chk->fs->source, 0, OReadOnly, 0);
721 	mr = sourceOpen(chk->fs->source, 1, OReadOnly, 0);
722 	sourceUnlock(chk->fs->source);
723 	chkDir(chk, "", r, mr);
724 
725 	sourceClose(r);
726 	sourceClose(mr);
727 }
728 
729 static void
730 setBit(uchar *bmap, u32int addr)
731 {
732 	if(addr == NilBlock)
733 		return;
734 
735 	bmap[addr>>3] |= 1 << (addr & 7);
736 }
737 
738 static int
739 getBit(uchar *bmap, u32int addr)
740 {
741 	if(addr == NilBlock)
742 		return 0;
743 
744 	return (bmap[addr>>3] >> (addr & 7)) & 1;
745 }
746 
747 static void
748 error(Fsck *chk, char *fmt, ...)
749 {
750 	char buf[256];
751 	va_list arg;
752 	static int nerr;
753 
754 	va_start(arg, fmt);
755 	vseprint(buf, buf+sizeof buf, fmt, arg);
756 	va_end(arg);
757 
758 	chk->print("error: %s\n", buf);
759 
760 //	if(nerr++ > 20)
761 //		vtFatal("too many errors");
762 }
763 
764 static void
765 warn(Fsck *chk, char *fmt, ...)
766 {
767 	char buf[256];
768 	va_list arg;
769 	static int nerr;
770 
771 	va_start(arg, fmt);
772 	vseprint(buf, buf+sizeof buf, fmt, arg);
773 	va_end(arg);
774 
775 	chk->print("error: %s\n", buf);
776 }
777 
778 static void
779 clrenop(Fsck*, Block*, int)
780 {
781 }
782 
783 static void
784 closenop(Fsck*, Block*, u32int)
785 {
786 }
787 
788 static void
789 clrinop(Fsck*, char*, MetaBlock*, int, Block*)
790 {
791 }
792 
793 static int
794 printnop(char*, ...)
795 {
796 	return 0;
797 }
798