1: #
   2:     char    *sccsid = "@(#)dcheck.c	2.3";
   3: 
   4: #include    <whoami.h>
   5: /*
   6:  * dcheck - check directory consistency
   7:  */
   8: #define NI  16
   9: #define NB  10
  10: #define NDIR    (BSIZE/sizeof(struct direct))
  11: 
  12: #include <stdio.h>
  13: #include <sys/param.h>
  14: #include <sys/inode.h>
  15: #include <sys/ino.h>
  16: #include <sys/dir.h>
  17: #include <sys/filsys.h>
  18: #include <sys/fblk.h>
  19: 
  20: 
  21: struct  filsys  sblock;
  22: struct  dinode  itab[INOPB*NI];
  23: daddr_t iaddr[NADDR];
  24: ino_t   ilist[NB];
  25: 
  26: int fi;
  27: ino_t   ino;
  28: char    *ecount;
  29: int headpr;
  30: unsigned    nfiles;
  31: 
  32: int nerror;
  33: daddr_t bmap();
  34: long    atol();
  35: char    *malloc();
  36: 
  37: main(argc, argv)
  38: char *argv[];
  39: {
  40:     register i;
  41:     long n;
  42: 
  43:     while (--argc) {
  44:         argv++;
  45:         if (**argv=='-')
  46:         switch ((*argv)[1]) {
  47: 
  48:         case 'i':
  49:             for(i=0; i<NB; i++) {
  50:                 n = atol(argv[1]);
  51:                 if(n == 0)
  52:                     break;
  53:                 ilist[i] = n;
  54:                 argv++;
  55:                 argc--;
  56:             }
  57:             ilist[i] = 0;
  58:             continue;
  59: 
  60:         default:
  61:             printf("Bad flag %c\n", (*argv)[1]);
  62:             nerror++;
  63:         }
  64:         check(*argv);
  65:     }
  66:     return(nerror);
  67: }
  68: 
  69: check(file)
  70: char *file;
  71: {
  72:     register i;
  73:     register j;
  74: 
  75:     fi = open(file, 0);
  76:     if(fi < 0) {
  77:         printf("cannot open %s\n", file);
  78:         nerror++;
  79:         return;
  80:     }
  81:     headpr = 0;
  82:     printf("%s:\n", file);
  83:     sync();
  84:     bread((daddr_t)1, (char *)&sblock, sizeof(sblock));
  85:     nfiles = (sblock.s_isize-2)*INOPB;
  86: #ifndef UCB_NKB
  87:     if (nfiles > 40000) {
  88:         printf("Only doing 40000 files\n");
  89:         nfiles = 40000;
  90:     }
  91: #else
  92:     if (nfiles > 30000) {
  93:         printf("Only doing 30000 files\n");
  94:         nfiles = 30000;
  95:     }
  96: #endif
  97:     ecount = malloc(nfiles+1);
  98:     if (ecount==NULL) {
  99:         printf("Not enough core\n");
 100:         exit(04);
 101:     }
 102:     for (i=0; i<=nfiles; i++)
 103:         ecount[i] = 0;
 104:     ino = 0;
 105:     for(i=2;; i+=NI) {
 106:         if(ino >= nfiles)
 107:             break;
 108:         bread((daddr_t)i, (char *)itab, sizeof(itab));
 109:         for(j=0; j<INOPB*NI; j++) {
 110:             if(ino >= nfiles)
 111:                 break;
 112:             ino++;
 113:             pass1(&itab[j]);
 114:         }
 115:     }
 116:     ino = 0;
 117:     for(i=2;; i+=NI) {
 118:         if(ino >= nfiles)
 119:             break;
 120:         bread((daddr_t)i, (char *)itab, sizeof(itab));
 121:         for(j=0; j<INOPB*NI; j++) {
 122:             if(ino >= nfiles)
 123:                 break;
 124:             ino++;
 125:             pass2(&itab[j]);
 126:         }
 127:     }
 128:     free(ecount);
 129: }
 130: 
 131: pass1(ip)
 132: register struct dinode *ip;
 133: {
 134:     struct direct dbuf[NDIR];
 135:     long doff;
 136:     struct direct *dp;
 137:     register i, j;
 138:     int k;
 139:     daddr_t d;
 140:     ino_t kno;
 141: 
 142:     if((ip->di_mode&IFMT) != IFDIR)
 143:         return;
 144:     l3tol(iaddr, ip->di_addr, NADDR);
 145:     doff = 0;
 146:     for(i=0;; i++) {
 147:         if(doff >= ip->di_size)
 148:             break;
 149:         d = bmap(i);
 150:         if(d == 0)
 151:             break;
 152:         bread(d, (char *)dbuf, BSIZE);
 153:         for(j=0; j<NDIR; j++) {
 154:             if(doff >= ip->di_size)
 155:                 break;
 156:             doff += sizeof(struct direct);
 157:             dp = &dbuf[j];
 158:             kno = dp->d_ino;
 159:             if(kno == 0)
 160:                 continue;
 161:             if(kno > nfiles || kno <= 1) {
 162:                 printf("%5u bad; %u/%.14s\n", kno, ino, dp->d_name);
 163:                 nerror++;
 164:                 continue;
 165:             }
 166:             for (k=0; ilist[k] != 0; k++)
 167:                 if (ilist[k]==kno) {
 168:                     printf("%5u arg; %u/%.14s\n", kno, ino, dp->d_name);
 169:                     nerror++;
 170:                 }
 171:             ecount[kno]++;
 172:             if (ecount[kno] == 0)
 173:                 ecount[kno] = 0377;
 174:         }
 175:     }
 176: }
 177: 
 178: pass2(ip)
 179: register struct dinode *ip;
 180: {
 181:     register i;
 182: 
 183:     i = ino;
 184:     if ((ip->di_mode&IFMT)==0 && ecount[i]==0)
 185:         return;
 186:     if (ip->di_nlink==((ecount[i])&0377) && ip->di_nlink!=0)
 187:         return;
 188:     if (ino < ROOTINO && ip->di_nlink==0 && ecount[i]==0)
 189:         return;
 190:     if (headpr==0) {
 191:         printf("     entries  link cnt\n");
 192:         headpr++;
 193:     }
 194:     printf("%u	%d	%d\n", ino,
 195:         ecount[i]&0377, ip->di_nlink);
 196: }
 197: 
 198: bread(bno, buf, cnt)
 199: daddr_t bno;
 200: char *buf;
 201: {
 202:     register i;
 203: 
 204:     lseek(fi, bno*BSIZE, 0);
 205:     if (read(fi, buf, cnt) != cnt) {
 206:         printf("read error %D\n", bno);
 207:         for(i=0; i<BSIZE; i++)
 208:             buf[i] = 0;
 209:     }
 210: }
 211: 
 212: 
 213: daddr_t
 214: bmap(i)
 215: {
 216:     daddr_t ibuf[NINDIR];
 217: 
 218:     if(i < NADDR-3)
 219:         return(iaddr[i]);
 220:     i -= NADDR-3;
 221:     if(i > NINDIR) {
 222:         printf("%u - huge directory\n", ino);
 223:         return((daddr_t)0);
 224:     }
 225:     bread(iaddr[NADDR-3], (char *)ibuf, sizeof(ibuf));
 226:     return(ibuf[i]);
 227: }

Defined functions

bmap defined in line 213; used 2 times
bread defined in line 198; used 5 times
check defined in line 69; used 1 times
  • in line 64
main defined in line 37; never used
pass1 defined in line 131; used 1 times
pass2 defined in line 178; used 1 times

Defined variables

ecount defined in line 28; used 11 times
fi defined in line 26; used 4 times
headpr defined in line 29; used 3 times
iaddr defined in line 23; used 3 times
ilist defined in line 24; used 4 times
ino defined in line 27; used 14 times
itab defined in line 22; used 6 times
nerror defined in line 32; used 5 times
nfiles defined in line 30; used 12 times
sblock defined in line 21; used 3 times
sccsid defined in line 2; never used

Defined macros

NB defined in line 9; used 2 times
NDIR defined in line 10; used 2 times
NI defined in line 8; used 5 times
Last modified: 1981-07-10
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1042
Valid CSS Valid XHTML 1.0 Strict