check.c 17 KB

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