Blob


1 #include "stdinc.h"
2 #include "dat.h"
3 #include "fns.h"
5 static int extra, missing, wrong;
7 static void
8 phdr(DBlock *eb)
9 {
10 static int did;
12 if(!did){
13 did = 1;
14 print("# diff actual correct\n");
15 }
16 print("%s block 0x%llux\n", eb->part->name, eb->addr);
17 }
19 static void
20 pie(IEntry *ie, char c)
21 {
22 print("%c %V %22lld %3d %5d %3d\n",
23 c, ie->score, ie->ia.addr, ie->ia.type, ie->ia.size, ie->ia.blocks);
24 }
26 static int
27 checkbucket(Index *ix, u32int buck, IBucket *ib)
28 {
29 ISect *is;
30 DBlock *eb;
31 IBucket eib;
32 IEntry ie, eie;
33 int i, ei, ok, c, hdr;
35 is = ix->sects[indexsect0(ix, buck)];
36 if(buck < is->start || buck >= is->stop){
37 seterr(EAdmin, "cannot find index section for bucket %lud\n", (ulong)buck);
38 return -1;
39 }
40 buck -= is->start;
41 eb = getdblock(is->part, is->blockbase + ((u64int)buck << is->blocklog), OREAD);
42 if(eb == nil)
43 return -1;
44 unpackibucket(&eib, eb->data, is->bucketmagic);
46 ok = 0;
47 ei = 0;
48 hdr = 0;
49 for(i = 0; i < ib->n; i++){
50 while(ei < eib.n){
51 c = ientrycmp(&ib->data[i * IEntrySize], &eib.data[ei * IEntrySize]);
52 if(c == 0){
53 unpackientry(&ie, &ib->data[i * IEntrySize]);
54 unpackientry(&eie, &eib.data[ei * IEntrySize]);
55 if(iaddrcmp(&ie.ia, &eie.ia) != 0){
56 if(!hdr){
57 phdr(eb);
58 hdr = 1;
59 }
60 wrong++;
61 pie(&eie, '<');
62 pie(&ie, '>');
63 }
64 ei++;
65 goto cont;
66 }
67 if(c < 0)
68 break;
69 if(!hdr){
70 phdr(eb);
71 hdr = 1;
72 }
73 unpackientry(&eie, &eib.data[ei*IEntrySize]);
74 extra++;
75 pie(&eie, '<');
76 ei++;
77 ok = -1;
78 }
79 if(!hdr){
80 phdr(eb);
81 hdr = 1;
82 }
83 unpackientry(&ie, &ib->data[i*IEntrySize]);
84 missing++;
85 pie(&ie, '>');
86 ok = -1;
87 cont:;
88 }
89 for(; ei < eib.n; ei++){
90 if(!hdr){
91 phdr(eb);
92 hdr = 1;
93 }
94 unpackientry(&eie, &eib.data[ei*IEntrySize]);
95 pie(&eie, '<');
96 ok = -1;
97 }
98 putdblock(eb);
99 return ok;
102 int
103 checkindex(Index *ix, Part *part, u64int off, u64int clumps, int zero)
105 IEStream *ies;
106 IBucket ib, zib;
107 ZBlock *z, *b;
108 u32int next, buck;
109 int ok, bok;
110 u64int found = 0;
112 //ZZZ make buffer size configurable
113 b = alloczblock(ix->blocksize, 0, ix->blocksize);
114 z = alloczblock(ix->blocksize, 1, ix->blocksize);
115 ies = initiestream(part, off, clumps, 64*1024);
116 if(b == nil || z == nil || ies == nil){
117 werrstr("allocating: %r");
118 ok = -1;
119 goto breakout;
120 return -1;
122 ok = 0;
123 next = 0;
124 memset(&ib, 0, sizeof ib);
125 ib.data = b->data;
126 zib.data = z->data;
127 zib.n = 0;
128 zib.buck = 0;
129 for(;;){
130 buck = buildbucket(ix, ies, &ib, ix->blocksize-IBucketSize);
131 found += ib.n;
132 if(zero){
133 for(; next != buck; next++){
134 if(next == ix->buckets){
135 if(buck != TWID32){
136 ok = -1;
137 werrstr("internal error: bucket out of range");
139 if(ok < 0)
140 werrstr("%d spurious entries, %d missing, %d wrong", extra, missing, wrong);
141 goto breakout;
143 bok = checkbucket(ix, next, &zib);
144 if(bok < 0)
145 ok = -1;
148 if(buck >= ix->buckets){
149 if(buck == TWID32)
150 break;
151 werrstr("internal error: bucket out of range");
152 ok = -1;
153 goto breakout;
155 bok = checkbucket(ix, buck, &ib);
156 if(bok < 0)
157 ok = -1;
158 next = buck + 1;
160 breakout:
161 freeiestream(ies);
162 freezblock(z);
163 freezblock(b);
164 return ok;
167 int
168 checkbloom(Bloom *b1, Bloom *b2, int fix)
170 u32int *a1, *a2;
171 int i, n, extra, missing;
173 if(b1==nil && b2==nil)
174 return 0;
175 if(b1==nil || b2==nil){
176 werrstr("nil/non-nil");
177 return -1;
179 wbbloomhead(b1);
180 wbbloomhead(b2);
181 if(memcmp(b1->data, b2->data, BloomHeadSize) != 0){
182 werrstr("bloom header mismatch");
183 return -1;
185 a1 = (u32int*)b1->data;
186 a2 = (u32int*)b2->data;
187 n = b1->size/4;
188 extra = 0;
189 missing = 0;
190 for(i=BloomHeadSize/4; i<n; i++){
191 if(a1[i] != a2[i]){
192 print("%.8ux/%.8ux.", a1[i], a2[i]);
193 extra += countbits(a1[i] & ~a2[i]);
194 missing += countbits(a2[i] & ~a1[i]);
197 if(extra || missing)
198 fprint(2, "bloom filter: %d spurious bits, %d missing bits\n", extra, missing);
199 else
200 fprint(2, "bloom filter: correct\n");
201 if(!fix && missing){
202 werrstr("missing bits");
203 return -1;
205 if(fix && (missing || extra)){
206 memmove(b1->data, b2->data, b1->size);
207 return writebloom(b1);
209 return 0;
213 void
214 usage(void)
216 fprint(2, "usage: checkindex [-f] [-B blockcachesize] config tmp\n");
217 threadexitsall(0);
220 Config conf;
222 void
223 threadmain(int argc, char *argv[])
225 Bloom *oldbloom, *newbloom;
226 Part *part;
227 u64int clumps, base;
228 u32int bcmem;
229 int fix, skipz, ok;
231 fix = 0;
232 bcmem = 0;
233 skipz = 0;
234 ARGBEGIN{
235 case 'B':
236 bcmem = unittoull(ARGF());
237 break;
238 case 'f':
239 fix++;
240 break;
241 case 'Z':
242 skipz = 1;
243 break;
244 default:
245 usage();
246 break;
247 }ARGEND
249 if(argc != 2)
250 usage();
252 ventifmtinstall();
254 part = initpart(argv[1], ORDWR|ODIRECT);
255 if(part == nil)
256 sysfatal("can't initialize temporary partition: %r");
258 if(!fix)
259 readonly = 1;
261 if(initventi(argv[0], &conf) < 0)
262 sysfatal("can't init venti: %r");
263 oldbloom = mainindex->bloom;
264 newbloom = nil;
265 if(oldbloom){
266 newbloom = vtmallocz(sizeof *newbloom);
267 bloominit(newbloom, oldbloom->size, nil);
268 newbloom->data = vtmallocz(oldbloom->size);
270 if(bcmem < maxblocksize * (mainindex->narenas + mainindex->nsects * 4 + 16))
271 bcmem = maxblocksize * (mainindex->narenas + mainindex->nsects * 4 + 16);
272 if(0) fprint(2, "initialize %d bytes of disk block cache\n", bcmem);
273 initdcache(bcmem);
275 fprint(2, "checkindex: building entry list\n");
276 clumps = sortrawientries(mainindex, part, &base, newbloom);
277 if(clumps == TWID64)
278 sysfatal("can't build sorted index: %r");
279 fprint(2, "checkindex: checking %lld entries at %lld\n", clumps, base);
280 ok = 0;
281 if(checkindex(mainindex, part, base, clumps, !skipz) < 0){
282 fprint(2, "checkindex: %r\n");
283 ok = -1;
285 if(checkbloom(oldbloom, newbloom, fix) < 0){
286 fprint(2, "checkbloom: %r\n");
287 ok = -1;
289 if(ok < 0)
290 sysfatal("errors found");
291 fprint(2, "checkindex: index is correct\n");
292 threadexitsall(0);