/* * Copyright (c) 1980 Regents of the University of California. * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. */ #ifndef lint static char sccsid[] = "@(#)icheck.c 5.5 (Berkeley) 07/30/89"; #endif not lint /* * icheck */ #define NB 500 #define MAXFN 500 #define MAXNINDIR (MAXBSIZE / sizeof (daddr_t)) #include #include #include #include #include #ifndef STANDALONE #include #endif union { struct fs sb; char pad[SBSIZE]; } sbun; #define sblock sbun.sb union { struct cg cg; char pad[MAXBSIZE]; } cgun; #define cgrp cgun.cg struct dinode itab[MAXBSIZE / sizeof(struct dinode)]; daddr_t blist[NB]; daddr_t fsblist[NB]; char *bmap; int mflg; int dflg; int fi; ino_t ino; int cginit; ino_t nrfile; ino_t ndfile; ino_t nbfile; ino_t ncfile; ino_t nlfile; ino_t nsfile; daddr_t nblock; daddr_t nfrag; daddr_t nindir; daddr_t niindir; daddr_t nffree; daddr_t nbfree; daddr_t ndup; int nerror; long dev_bsize = 1; long atol(); #ifndef STANDALONE char *malloc(); char *calloc(); #endif main(argc, argv) int argc; char *argv[]; { register i; long n; blist[0] = -1; #ifndef STANDALONE while (--argc) { argv++; if (**argv=='-') switch ((*argv)[1]) { case 'd': dflg++; continue; case 'm': mflg++; continue; case 'b': for(i=0; idi_mode & IFMT; if(i == 0) return; switch (i) { case IFCHR: ncfile++; return; case IFBLK: nbfile++; return; case IFDIR: ndfile++; break; case IFREG: nrfile++; break; case IFSOCK: nsfile++; break; case IFLNK: nlfile++; break; default: printf("bad mode %u\n", ino); return; } for (i = 0; i < NDADDR; i++) { db = ip->di_db[i]; if (db == 0) continue; siz = dblksize(&sblock, ip, i); (void)sprintf(buf, "logical data block %d", i); chk(db, buf, siz); if (siz == sblock.fs_bsize) nblock++; else nfrag += howmany(siz, sblock.fs_fsize); } for(i = 0; i < NIADDR; i++) { ib = ip->di_ib[i]; if (ib == 0) continue; if (chk(ib, "1st indirect", sblock.fs_bsize)) continue; bread(fsbtodb(&sblock, ib), (char *)ind1, sblock.fs_bsize); nindir++; for (j = 0; j < NINDIR(&sblock); j++) { ib = ind1[j]; if (ib == 0) continue; if (i == 0) { lbn = NDADDR + j; siz = dblksize(&sblock, ip, lbn); (void)sprintf(buf, "logical data block %d", lbn); chk(ib, buf, siz); if (siz == sblock.fs_bsize) nblock++; else nfrag += howmany(siz, sblock.fs_fsize); continue; } if (chk(ib, "2nd indirect", sblock.fs_bsize)) continue; bread(fsbtodb(&sblock, ib), (char *)ind2, sblock.fs_bsize); niindir++; for (k = 0; k < NINDIR(&sblock); k++) { ib = ind2[k]; if (ib == 0) continue; lbn = NDADDR + NINDIR(&sblock) * (i + j) + k; siz = dblksize(&sblock, ip, lbn); (void)sprintf(buf, "logical data block %d", lbn); chk(ib, buf, siz); if (siz == sblock.fs_bsize) nblock++; else nfrag += howmany(siz, sblock.fs_fsize); } } } } chk(bno, s, size) daddr_t bno; char *s; int size; { register n, cg; int frags; cg = dtog(&sblock, bno); if (cginit == 0 && bno >= sblock.fs_frag * sblock.fs_size) { printf("%ld bad; inode=%u, class=%s\n", bno, ino, s); return(1); } frags = numfrags(&sblock, size); if (frags == sblock.fs_frag) { if (duped(bno, size)) { printf("%ld dup block; inode=%u, class=%s\n", bno, ino, s); ndup += sblock.fs_frag; } } else { for (n = 0; n < frags; n++) { if (duped(bno + n, sblock.fs_fsize)) { printf("%ld dup frag; inode=%u, class=%s\n", bno, ino, s); ndup++; } } } for (n=0; blist[n] != -1; n++) if (fsblist[n] >= bno && fsblist[n] < bno + frags) printf("%ld arg; frag %d of %d, inode=%u, class=%s\n", blist[n], fsblist[n] - bno, frags, ino, s); return(0); } duped(bno, size) daddr_t bno; int size; { if(dflg) return(0); if (size != sblock.fs_fsize && size != sblock.fs_bsize) printf("bad size %d to duped\n", size); if (size == sblock.fs_fsize) { if (isset(bmap, bno)) return(1); setbit(bmap, bno); return (0); } if (bno % sblock.fs_frag != 0) printf("bad bno %d to duped\n", bno); if (isblock(&sblock, bmap, bno/sblock.fs_frag)) return (1); setblock(&sblock, bmap, bno/sblock.fs_frag); return(0); } getsb(fs, file) register struct fs *fs; char *file; { int i, j, size; if (bread(SBOFF, fs, SBSIZE)) { printf("bad super block"); perror(file); nerror |= 04; return; } if (fs->fs_magic != FS_MAGIC) { printf("%s: bad magic number\n", file); nerror |= 04; return; } dev_bsize = fs->fs_fsize / fsbtodb(fs, 1); for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { size = sblock.fs_cssize - i < sblock.fs_bsize ? sblock.fs_cssize - i : sblock.fs_bsize; sblock.fs_csp[j] = (struct csum *)calloc(1, size); bread(fsbtodb(fs, fs->fs_csaddr + (j * fs->fs_frag)), (char *)fs->fs_csp[j], size); } } bread(bno, buf, cnt) daddr_t bno; char *buf; { register i; lseek(fi, bno * dev_bsize, 0); if ((i = read(fi, buf, cnt)) != cnt) { for(i=0; ifs_frag) { case 8: return (cp[h] == 0xff); case 4: mask = 0x0f << ((h & 0x1) << 2); return ((cp[h >> 1] & mask) == mask); case 2: mask = 0x03 << ((h & 0x3) << 1); return ((cp[h >> 2] & mask) == mask); case 1: mask = 0x01 << (h & 0x7); return ((cp[h >> 3] & mask) == mask); default: #ifdef STANDALONE printf("isblock bad fs_frag %d\n", fs->fs_frag); #else fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag); #endif return; } } /* * put a block into the map */ setblock(fs, cp, h) struct fs *fs; unsigned char *cp; int h; { switch (fs->fs_frag) { case 8: cp[h] = 0xff; return; case 4: cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); return; case 2: cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); return; case 1: cp[h >> 3] |= (0x01 << (h & 0x7)); return; default: #ifdef STANDALONE printf("setblock bad fs_frag %d\n", fs->fs_frag); #else fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag); #endif return; } }