33 static Devsize sbaddr;
34 static Devsize oldblock;
38 static uchar *lowstack, *startstack;
40 /* local prototypes */
41 static int amark(Off);
42 static void* chkalloc(ulong);
43 static void ckfreelist(Superb*);
44 static int fmark(Off);
45 static int fsck(Dentry*);
46 static int ftest(Off);
47 static Dentry* maked(Off, int, Off);
48 static void missing(void);
49 static void mkfreelist(Superb*);
50 static void modd(Off, int, Dentry*);
51 static void qmark(Off);
52 static void trfreelist(Superb*);
53 static void xaddfree(Device*, Off, Superb*, Iobuf*);
54 static void xflush(Device*, Superb*, Iobuf*);
55 static void xread(Off, Off);
56 static Iobuf* xtag(Off, int, Off);
61 char *p = mallocz(n, 1);
64 panic("chkalloc: out of memory");
79 Crdall = (1<<0), /* read all files */
80 Ctag = (1<<1), /* rebuild tags */
81 Cpfile = (1<<2), /* print files */
82 Cpdir = (1<<3), /* print directories */
83 Cfree = (1<<4), /* rebuild free list */
84 // Csetqid = (1<<5), /* resequence qids */
85 Cream = (1<<6), /* clear all bad tags */
86 Cbad = (1<<7), /* clear all bad blocks */
87 Ctouch = (1<<8), /* touch old dir and indir */
88 Ctrim = (1<<9), /* trim fsize back to fit when checking free list */
100 // "setqid", Csetqid,
109 cmd_check(int argc, char *argv[])
119 for(i=1; i<argc; i++) {
120 for(f=0; ckoption[f].option; f++)
121 if(strcmp(argv[i], ckoption[f].option) == 0)
123 print("unknown check option %s\n", argv[i]);
124 for(f=0; ckoption[f].option; f++)
125 print("\t%s\n", ckoption[f].option);
128 flag |= ckoption[f].flag;
133 ronly = (dev->type == Devro);
134 cwflag = (dev->type == Devcw) | (dev->type == Devro);
136 wlock(&mainlock); /* check */
139 name = abits = qbits = nil; /* in case of goto */
140 sbaddr = superaddr(dev);
141 raddr = getraddr(dev);
142 p = xtag(sbaddr, Tsuper, QPSUPER);
145 sb = (Superb*)p->iobuf;
149 /* 200 is slop since qidgen is likely to be a little bit low */
150 sizqbits = (sb->qidgen+200 + 7) / 8;
151 qbits = chkalloc(sizqbits);
154 sizabits = (fsize-fstart + 7)/8;
155 abits = chkalloc(sizabits);
158 name = chkalloc(sizname);
159 sizname -= NAMELEN+10; /* for safety */
172 /* round fsize down to start of current side */
177 for (s = 0; dsize = wormsizeside(dev, s),
178 dsize > 0 && oldblock + dsize < fsize; s++)
180 print("oldblock = %lld\n", (Wideoff)oldblock);
188 print("checking filsys: %s\n", fs->name);
192 d = maked(raddr, 0, QPROOT);
200 print("depth not zero on return\n");
209 if(sb->qidgen < maxq)
210 print("qid generator low path=%lld maxq=%lld\n",
211 (Wideoff)sb->qidgen, (Wideoff)maxq);
216 print("file system was modified\n");
217 settag(p, Tsuper, QPNONE);
220 print("nfiles = %lld\n", (Wideoff)nfiles);
221 print("fsize = %lld\n", (Wideoff)fsize);
222 print("nused = %lld\n", (Wideoff)nused);
223 print("ndup = %lld\n", (Wideoff)ndup);
224 print("nfree = %lld\n", (Wideoff)nfree);
225 print("tfree = %lld\n", (Wideoff)sb->tfree);
226 print("nfdup = %lld\n", (Wideoff)nfdup);
227 print("nmiss = %lld\n", (Wideoff)fsize-fstart-nused-nfree);
228 print("nbad = %lld\n", (Wideoff)nbad);
229 print("nqbad = %lld\n", (Wideoff)nqbad);
230 print("maxq = %lld\n", (Wideoff)maxq);
231 print("base stack=%llud\n", (vlong)startstack);
232 /* high-water mark of stack usage */
233 print("high stack=%llud\n", (vlong)lowstack);
234 print("deepest recursion=%d\n", maxdepth-1); /* one-origin */
244 name = abits = qbits = nil;
250 * if *blkp is already allocated and Cbad is set, zero it.
251 * returns *blkp if it's free, else 0.
254 blkck(Off *blkp, int *flgp)
269 * if a block address within a Dentry, *blkp, is already allocated
270 * and Cbad is set, zero it.
271 * stores 0 into *resp if already allocated, else stores *blkp.
272 * returns dmod count.
275 daddrck(Off *blkp, Off *resp)
291 * under Ctouch, read block `a' if it's in range.
292 * returns dmod count.
297 if((flags&Ctouch) && a < oldblock) {
298 Iobuf *pd = getbuf(dev, a, Brd|Bmod);
308 * if d is a directory, touch it and check all its entries in block a.
309 * if not, under Crdall, read a.
310 * returns dmod count.
313 dirck(Extdentry *ed, Off a)
317 if(ed->d->mode & DDIR) {
319 for(k=0; k<DIRPERBUF; k++) {
320 Dentry *nd = maked(a, k, ed->qpath);
332 } else if(flags & Crdall)
338 * touch a, check a's tag for Tind1, Tind2, etc.
339 * if the tag is right, validate each block number in the indirect block,
340 * and check each block (mostly in case we are reading a huge directory).
343 indirck(Extdentry *ed, Off a, int tag)
351 if (p1 = xtag(a, tag, ed->qpath)) {
352 for(i=0; i<INDPERBUF; i++) {
353 a = blkck(&((Off *)p1->iobuf)[i], &p1->flags);
356 * check each block named in this
357 * indirect(^n) block (a).
360 dmod += dirck(ed, a);
362 dmod += indirck(ed, a, tag-1);
370 indiraddrck(Extdentry *ed, Off *indirp, int tag)
375 dmod = daddrck(indirp, &a);
376 return dmod + indirck(ed, a, tag);
379 /* if result is true, *d was modified */
387 if(depth >= maxdepth)
390 startstack = lowstack = (uchar *)&edent;
391 /* more precise check, assumes downward-growing stack */
392 if ((uchar *)&edent < lowstack)
393 lowstack = (uchar *)&edent;
395 /* check that entry is allocated */
396 if(!(d->mode & DALLOC))
400 /* we stash qpath & ns in an Extdentry for eventual use by dirck() */
401 memset(&edent, 0, sizeof edent);
405 edent.ns = strlen(name);
408 d->name[NAMELEN-1] = 0;
409 print("%s->name (%s) not terminated\n", name, d->name);
413 if(edent.ns >= sizname) {
414 print("%s->name (%s) name too large\n", name, d->name);
417 strcat(name, d->name);
428 } else if(flags & Cpfile) {
434 edent.qpath = d->qid.path & ~QPDIR;
436 if(edent.qpath > maxq)
439 /* check direct blocks (the common case) */
444 for(i=0; i<NDBLOCK; i++) {
445 dmod += daddrck(&d->dblock[i], &a);
447 dmod += dirck(&edent, a);
450 /* check indirect^n blocks, if any */
451 for (i = 0; i < NIBLOCK; i++)
452 dmod += indiraddrck(&edent, &d->iblocks[i], Tind1+i);
456 enum { XFEN = FEPERBUF + 6 };
466 xaddfree(Device *dev, Off a, Superb *sb, Iobuf *p)
470 x = (Xfree*)p->iobuf;
471 if(x->count < XFEN) {
472 x->addr[x->count] = a;
477 memset(&sb->fbuf, 0, sizeof(sb->fbuf));
478 sb->fbuf.free[0] = 0L;
487 xflush(Device *dev, Superb *sb, Iobuf *p)
492 x = (Xfree*)p->iobuf;
494 memset(&sb->fbuf, 0, sizeof(sb->fbuf));
495 sb->fbuf.free[0] = 0L;
499 for(i=0; i<x->count; i++)
500 addfree(dev, x->addr[i], sb);
505 * from existing freelist
509 trfreelist(Superb *sb)
517 xp = getbuf(devnone, Cckbuf, 0);
518 memset(xp->iobuf, 0, BUFSIZE);
523 if(n < 0 || n > FEPERBUF)
528 xaddfree(dev, a, sb, xp);
535 xaddfree(dev, a, sb, xp);
538 p = xtag(a, Tfree, QPNONE);
541 fb = (Fbuf*)p->iobuf;
548 print("%lld blocks free\n", (Wideoff)sb->tfree);
552 ckfreelist(Superb *sb)
559 strcpy(name, "free list");
560 print("check %s\n", name);
568 if(n < 0 || n > FEPERBUF) {
569 print("check: nfree bad %lld\n", (Wideoff)a);
592 p = xtag(a, Tfree, QPNONE);
595 fb = (Fbuf*)p->iobuf;
600 fsize = hi--; /* fsize = hi + 1 */
603 print("set fsize to %lld\n", (Wideoff)fsize);
605 print("lo = %lld; hi = %lld\n", (Wideoff)lo, (Wideoff)hi);
609 * make freelist from scratch
612 mkfreelist(Superb *sb)
618 print("cant make freelist on ronly device\n");
621 strcpy(name, "free list");
622 memset(&sb->fbuf, 0, sizeof(sb->fbuf));
623 sb->fbuf.free[0] = 0L;
626 for(a=fsize-fstart-1; a >= 0; a--) {
628 if(i < 0 || i >= sizabits)
633 addfree(dev, fstart+a, sb);
635 print("%lld blocks free\n", (Wideoff)sb->tfree);
640 maked(Off a, int s, Off qpath)
645 p = xtag(a, Tdir, qpath);
649 d1 = chkalloc(sizeof(Dentry));
650 memmove(d1, d, sizeof(Dentry));
656 modd(Off a, int s, Dentry *d1)
663 p = getbuf(dev, a, Brd);
670 memmove(d, d1, sizeof(Dentry));
676 xread(Off a, Off qpath)
680 p = xtag(a, Tfile, qpath);
686 xtag(Off a, int tag, Off qpath)
692 p = getbuf(dev, a, Brd);
694 print("check: \"%s\": xtag: p null\n", name);
695 if(flags & (Cream|Ctag)) {
696 p = getbuf(dev, a, Bmod);
698 memset(p->iobuf, 0, RBUFSIZE);
699 settag(p, tag, qpath);
706 if(checktag(p, tag, qpath)) {
707 print("check: \"%s\": xtag: checktag\n", name);
708 if(flags & (Cream|Ctag)) {
710 memset(p->iobuf, 0, RBUFSIZE);
711 settag(p, tag, qpath);
726 if(a < fstart || a >= fsize) {
729 print("check: \"%s\": range %lld\n",
740 print("check: \"%s\": address dup %lld\n",
741 name, (Wideoff)fstart+a);
758 if(a < fstart || a >= fsize) {
759 print("check: \"%s\": range %lld\n",
768 print("check: \"%s\": address dup %lld\n",
769 name, (Wideoff)fstart+a);
784 if(a < fstart || a >= fsize)
802 for(a=fsize-fstart-1; a>=0; a--) {
805 if(!(abits[i] & b)) {
806 print("missing: %lld\n", (Wideoff)fstart+a);
824 if(i < 0 || i >= sizqbits) {
827 print("check: \"%s\": qid out of range %llux\n",
828 name, (Wideoff)qpath);
831 if((qbits[i] & b) && !ronly) {
834 print("check: \"%s\": qid dup %llux\n", name,