122053Sdist /* 2*61492Sbostic * Copyright (c) 1980, 1986, 1993 3*61492Sbostic * The Regents of the University of California. All rights reserved. 439976Smckusick * 542702Sbostic * %sccs.include.redist.c% 622053Sdist */ 722053Sdist 816267Smckusick #ifndef lint 9*61492Sbostic static char sccsid[] = "@(#)pass5.c 8.1 (Berkeley) 06/05/93"; 1039976Smckusick #endif /* not lint */ 1116267Smckusick 1216267Smckusick #include <sys/param.h> 1353703Smckusick #include <sys/time.h> 1451532Sbostic #include <ufs/ufs/dinode.h> 1551532Sbostic #include <ufs/ffs/fs.h> 1644934Smckusick #include <string.h> 1716267Smckusick #include "fsck.h" 1816267Smckusick 1916267Smckusick pass5() 2016267Smckusick { 2134141Smckusick int c, blk, frags, basesize, sumsize, mapsize, savednrpos; 2234223Smckusick register struct fs *fs = &sblock; 2334223Smckusick register struct cg *cg = &cgrp; 2434141Smckusick daddr_t dbase, dmax; 2534141Smckusick register daddr_t d; 2617937Smckusick register long i, j; 2717937Smckusick struct csum *cs; 2817937Smckusick struct csum cstotal; 2937099Smckusick struct inodesc idesc[3]; 3017937Smckusick char buf[MAXBSIZE]; 3117937Smckusick register struct cg *newcg = (struct cg *)buf; 3234141Smckusick struct ocg *ocg = (struct ocg *)buf; 3316267Smckusick 3444934Smckusick bzero((char *)newcg, (size_t)fs->fs_cgsize); 3534223Smckusick newcg->cg_niblk = fs->fs_ipg; 3639973Smckusick switch ((int)fs->fs_postblformat) { 3734141Smckusick 3834141Smckusick case FS_42POSTBLFMT: 3934141Smckusick basesize = (char *)(&ocg->cg_btot[0]) - (char *)(&ocg->cg_link); 4034141Smckusick sumsize = &ocg->cg_iused[0] - (char *)(&ocg->cg_btot[0]); 4134223Smckusick mapsize = &ocg->cg_free[howmany(fs->fs_fpg, NBBY)] - 4234141Smckusick (u_char *)&ocg->cg_iused[0]; 4334141Smckusick ocg->cg_magic = CG_MAGIC; 4434223Smckusick savednrpos = fs->fs_nrpos; 4534223Smckusick fs->fs_nrpos = 8; 4634141Smckusick break; 4734141Smckusick 4834141Smckusick case FS_DYNAMICPOSTBLFMT: 4934141Smckusick newcg->cg_btotoff = 5034141Smckusick &newcg->cg_space[0] - (u_char *)(&newcg->cg_link); 5134141Smckusick newcg->cg_boff = 5234223Smckusick newcg->cg_btotoff + fs->fs_cpg * sizeof(long); 5334141Smckusick newcg->cg_iusedoff = newcg->cg_boff + 5434223Smckusick fs->fs_cpg * fs->fs_nrpos * sizeof(short); 5534141Smckusick newcg->cg_freeoff = 5634223Smckusick newcg->cg_iusedoff + howmany(fs->fs_ipg, NBBY); 5734141Smckusick newcg->cg_nextfreeoff = newcg->cg_freeoff + 5834223Smckusick howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs), 5934141Smckusick NBBY); 6034141Smckusick newcg->cg_magic = CG_MAGIC; 6134141Smckusick basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_link); 6234141Smckusick sumsize = newcg->cg_iusedoff - newcg->cg_btotoff; 6334141Smckusick mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff; 6434141Smckusick break; 6534141Smckusick 6634141Smckusick default: 6734141Smckusick errexit("UNKNOWN ROTATIONAL TABLE FORMAT %d\n", 6834223Smckusick fs->fs_postblformat); 6934141Smckusick } 7037099Smckusick bzero((char *)&idesc[0], sizeof idesc); 7154601Smckusick for (i = 0; i < 3; i++) { 7237099Smckusick idesc[i].id_type = ADDR; 7354601Smckusick if (doinglevel2) 7454601Smckusick idesc[i].id_fix = FIX; 7554601Smckusick } 7617937Smckusick bzero((char *)&cstotal, sizeof(struct csum)); 7740018Smckusick j = blknum(fs, fs->fs_size + fs->fs_frag - 1); 7840018Smckusick for (i = fs->fs_size; i < j; i++) 7934141Smckusick setbmap(i); 8034223Smckusick for (c = 0; c < fs->fs_ncg; c++) { 8134223Smckusick getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize); 8234223Smckusick if (!cg_chkmagic(cg)) 8316267Smckusick pfatal("CG %d: BAD MAGIC NUMBER\n", c); 8434223Smckusick dbase = cgbase(fs, c); 8534223Smckusick dmax = dbase + fs->fs_fpg; 8634223Smckusick if (dmax > fs->fs_size) 8734223Smckusick dmax = fs->fs_size; 8856353Smckusick newcg->cg_time = cg->cg_time; 8917937Smckusick newcg->cg_cgx = c; 9034223Smckusick if (c == fs->fs_ncg - 1) 9134223Smckusick newcg->cg_ncyl = fs->fs_ncyl % fs->fs_cpg; 9217937Smckusick else 9334223Smckusick newcg->cg_ncyl = fs->fs_cpg; 9417937Smckusick newcg->cg_ndblk = dmax - dbase; 9517937Smckusick newcg->cg_cs.cs_ndir = 0; 9617937Smckusick newcg->cg_cs.cs_nffree = 0; 9717937Smckusick newcg->cg_cs.cs_nbfree = 0; 9834223Smckusick newcg->cg_cs.cs_nifree = fs->fs_ipg; 9934223Smckusick if (cg->cg_rotor < newcg->cg_ndblk) 10034223Smckusick newcg->cg_rotor = cg->cg_rotor; 10117937Smckusick else 10217937Smckusick newcg->cg_rotor = 0; 10334223Smckusick if (cg->cg_frotor < newcg->cg_ndblk) 10434223Smckusick newcg->cg_frotor = cg->cg_frotor; 10517937Smckusick else 10617937Smckusick newcg->cg_frotor = 0; 10734223Smckusick if (cg->cg_irotor < newcg->cg_niblk) 10834223Smckusick newcg->cg_irotor = cg->cg_irotor; 10917937Smckusick else 11017937Smckusick newcg->cg_irotor = 0; 11134141Smckusick bzero((char *)&newcg->cg_frsum[0], sizeof newcg->cg_frsum); 11244934Smckusick bzero((char *)&cg_blktot(newcg)[0], 11344934Smckusick (size_t)(sumsize + mapsize)); 11434223Smckusick if (fs->fs_postblformat == FS_42POSTBLFMT) 11534141Smckusick ocg->cg_magic = CG_MAGIC; 11634223Smckusick j = fs->fs_ipg * c; 11734223Smckusick for (i = 0; i < fs->fs_ipg; j++, i++) { 11817937Smckusick switch (statemap[j]) { 11917937Smckusick 12017937Smckusick case USTATE: 12117937Smckusick break; 12217937Smckusick 12317937Smckusick case DSTATE: 12417937Smckusick case DCLEAR: 12517937Smckusick case DFOUND: 12617937Smckusick newcg->cg_cs.cs_ndir++; 12717937Smckusick /* fall through */ 12817937Smckusick 12917937Smckusick case FSTATE: 13017937Smckusick case FCLEAR: 13117937Smckusick newcg->cg_cs.cs_nifree--; 13234141Smckusick setbit(cg_inosused(newcg), i); 13317937Smckusick break; 13426480Smckusick 13526480Smckusick default: 13626480Smckusick if (j < ROOTINO) 13726480Smckusick break; 13826480Smckusick errexit("BAD STATE %d FOR INODE I=%d", 13926480Smckusick statemap[j], j); 14017937Smckusick } 14116267Smckusick } 14217937Smckusick if (c == 0) 14317937Smckusick for (i = 0; i < ROOTINO; i++) { 14434141Smckusick setbit(cg_inosused(newcg), i); 14517937Smckusick newcg->cg_cs.cs_nifree--; 14616267Smckusick } 14717937Smckusick for (i = 0, d = dbase; 14834141Smckusick d < dmax; 14934223Smckusick d += fs->fs_frag, i += fs->fs_frag) { 15017937Smckusick frags = 0; 15134223Smckusick for (j = 0; j < fs->fs_frag; j++) { 15239973Smckusick if (testbmap(d + j)) 15317937Smckusick continue; 15434141Smckusick setbit(cg_blksfree(newcg), i + j); 15517937Smckusick frags++; 15617937Smckusick } 15734223Smckusick if (frags == fs->fs_frag) { 15817937Smckusick newcg->cg_cs.cs_nbfree++; 15934223Smckusick j = cbtocylno(fs, i); 16034141Smckusick cg_blktot(newcg)[j]++; 16134223Smckusick cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++; 16217937Smckusick } else if (frags > 0) { 16317937Smckusick newcg->cg_cs.cs_nffree += frags; 16434223Smckusick blk = blkmap(fs, cg_blksfree(newcg), i); 16551637Sbostic ffs_fragacct(fs, blk, newcg->cg_frsum, 1); 16617937Smckusick } 16716267Smckusick } 16817937Smckusick cstotal.cs_nffree += newcg->cg_cs.cs_nffree; 16917937Smckusick cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree; 17017937Smckusick cstotal.cs_nifree += newcg->cg_cs.cs_nifree; 17117937Smckusick cstotal.cs_ndir += newcg->cg_cs.cs_ndir; 17234223Smckusick cs = &fs->fs_cs(fs, c); 17334141Smckusick if (bcmp((char *)&newcg->cg_cs, (char *)cs, sizeof *cs) != 0 && 17437099Smckusick dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { 17534141Smckusick bcopy((char *)&newcg->cg_cs, (char *)cs, sizeof *cs); 17634141Smckusick sbdirty(); 17734141Smckusick } 17854601Smckusick if (doinglevel1) { 17944934Smckusick bcopy((char *)newcg, (char *)cg, (size_t)fs->fs_cgsize); 18034141Smckusick cgdirty(); 18134141Smckusick continue; 18234141Smckusick } 18334141Smckusick if (bcmp(cg_inosused(newcg), 18434223Smckusick cg_inosused(cg), mapsize) != 0 && 18537099Smckusick dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) { 18644934Smckusick bcopy(cg_inosused(newcg), cg_inosused(cg), 18744934Smckusick (size_t)mapsize); 18817937Smckusick cgdirty(); 18916267Smckusick } 19034223Smckusick if ((bcmp((char *)newcg, (char *)cg, basesize) != 0 || 19134141Smckusick bcmp((char *)&cg_blktot(newcg)[0], 19234223Smckusick (char *)&cg_blktot(cg)[0], sumsize) != 0) && 19337099Smckusick dofix(&idesc[2], "SUMMARY INFORMATION BAD")) { 19444934Smckusick bcopy((char *)newcg, (char *)cg, (size_t)basesize); 19534141Smckusick bcopy((char *)&cg_blktot(newcg)[0], 19644934Smckusick (char *)&cg_blktot(cg)[0], (size_t)sumsize); 19717937Smckusick cgdirty(); 19816267Smckusick } 19916267Smckusick } 20034223Smckusick if (fs->fs_postblformat == FS_42POSTBLFMT) 20134223Smckusick fs->fs_nrpos = savednrpos; 20234223Smckusick if (bcmp((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs) != 0 20337099Smckusick && dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { 20434223Smckusick bcopy((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs); 20534223Smckusick fs->fs_ronly = 0; 20634223Smckusick fs->fs_fmod = 0; 20717937Smckusick sbdirty(); 20816267Smckusick } 20916267Smckusick } 210