1*22053Sdist /* 2*22053Sdist * Copyright (c) 1980 Regents of the University of California. 3*22053Sdist * All rights reserved. The Berkeley software License Agreement 4*22053Sdist * specifies the terms and conditions for redistribution. 5*22053Sdist */ 6*22053Sdist 716267Smckusick #ifndef lint 8*22053Sdist static char sccsid[] = "@(#)pass5.c 5.1 (Berkeley) 06/05/85"; 9*22053Sdist #endif not lint 1016267Smckusick 1116267Smckusick #include <sys/param.h> 1216267Smckusick #include <sys/inode.h> 1316267Smckusick #include <sys/fs.h> 1416267Smckusick #include "fsck.h" 1516267Smckusick 1616267Smckusick pass5() 1716267Smckusick { 1817937Smckusick int c, blk, frags, sumsize, mapsize; 1917937Smckusick daddr_t dbase, dmax, d; 2017937Smckusick register long i, j; 2117937Smckusick struct csum *cs; 2217937Smckusick time_t now; 2317937Smckusick struct csum cstotal; 2417937Smckusick struct inodesc idesc; 2517937Smckusick char buf[MAXBSIZE]; 2617937Smckusick register struct cg *newcg = (struct cg *)buf; 2716267Smckusick 2817937Smckusick bzero((char *)newcg, sblock.fs_cgsize); 2917937Smckusick newcg->cg_magic = CG_MAGIC; 3017937Smckusick bzero((char *)&idesc, sizeof(struct inodesc)); 3117937Smckusick idesc.id_type = ADDR; 3217937Smckusick bzero((char *)&cstotal, sizeof(struct csum)); 3317937Smckusick sumsize = cgrp.cg_iused - (char *)(&cgrp); 3418667Smckusick mapsize = &cgrp.cg_free[howmany(sblock.fs_fpg, NBBY)] - 3518667Smckusick (u_char *)cgrp.cg_iused; 3617937Smckusick (void)time(&now); 3716267Smckusick for (c = 0; c < sblock.fs_ncg; c++) { 3821540Smckusick getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize); 3917937Smckusick if (cgrp.cg_magic != CG_MAGIC) 4016267Smckusick pfatal("CG %d: BAD MAGIC NUMBER\n", c); 4117937Smckusick dbase = cgbase(&sblock, c); 4217937Smckusick dmax = dbase + sblock.fs_fpg; 4317937Smckusick if (dmax > sblock.fs_size) 4417937Smckusick dmax = sblock.fs_size; 4517937Smckusick if (now > cgrp.cg_time) 4617937Smckusick newcg->cg_time = cgrp.cg_time; 4717937Smckusick else 4817937Smckusick newcg->cg_time = now; 4917937Smckusick newcg->cg_cgx = c; 5017937Smckusick if (c == sblock.fs_ncg - 1) 5117937Smckusick newcg->cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg; 5217937Smckusick else 5317937Smckusick newcg->cg_ncyl = sblock.fs_cpg; 5417937Smckusick newcg->cg_niblk = sblock.fs_ipg; 5517937Smckusick newcg->cg_ndblk = dmax - dbase; 5617937Smckusick newcg->cg_cs.cs_ndir = 0; 5717937Smckusick newcg->cg_cs.cs_nffree = 0; 5817937Smckusick newcg->cg_cs.cs_nbfree = 0; 5917937Smckusick newcg->cg_cs.cs_nifree = sblock.fs_ipg; 6017937Smckusick if (cgrp.cg_rotor < newcg->cg_ndblk) 6117937Smckusick newcg->cg_rotor = cgrp.cg_rotor; 6217937Smckusick else 6317937Smckusick newcg->cg_rotor = 0; 6417937Smckusick if (cgrp.cg_frotor < newcg->cg_ndblk) 6517937Smckusick newcg->cg_frotor = cgrp.cg_frotor; 6617937Smckusick else 6717937Smckusick newcg->cg_frotor = 0; 6817937Smckusick if (cgrp.cg_irotor < newcg->cg_niblk) 6917937Smckusick newcg->cg_irotor = cgrp.cg_irotor; 7017937Smckusick else 7117937Smckusick newcg->cg_irotor = 0; 7217937Smckusick bzero((char *)newcg->cg_frsum, sizeof newcg->cg_frsum); 7317937Smckusick bzero((char *)newcg->cg_btot, sizeof newcg->cg_btot); 7417937Smckusick bzero((char *)newcg->cg_b, sizeof newcg->cg_b); 7517937Smckusick bzero((char *)newcg->cg_free, howmany(sblock.fs_fpg, NBBY)); 7617937Smckusick bzero((char *)newcg->cg_iused, howmany(sblock.fs_ipg, NBBY)); 7717937Smckusick j = sblock.fs_ipg * c; 7817937Smckusick for (i = 0; i < sblock.fs_ipg; j++, i++) { 7917937Smckusick switch (statemap[j]) { 8017937Smckusick 8117937Smckusick case USTATE: 8217937Smckusick break; 8317937Smckusick 8417937Smckusick case DSTATE: 8517937Smckusick case DCLEAR: 8617937Smckusick case DFOUND: 8717937Smckusick newcg->cg_cs.cs_ndir++; 8817937Smckusick /* fall through */ 8917937Smckusick 9017937Smckusick case FSTATE: 9117937Smckusick case FCLEAR: 9217937Smckusick newcg->cg_cs.cs_nifree--; 9317937Smckusick setbit(newcg->cg_iused, i); 9417937Smckusick break; 9517937Smckusick } 9616267Smckusick } 9717937Smckusick if (c == 0) 9817937Smckusick for (i = 0; i < ROOTINO; i++) { 9917937Smckusick setbit(newcg->cg_iused, i); 10017937Smckusick newcg->cg_cs.cs_nifree--; 10116267Smckusick } 10217937Smckusick for (i = 0, d = dbase; 10317937Smckusick d <= dmax - sblock.fs_frag; 10417937Smckusick d += sblock.fs_frag, i += sblock.fs_frag) { 10517937Smckusick frags = 0; 10617937Smckusick for (j = 0; j < sblock.fs_frag; j++) { 10717937Smckusick if (getbmap(d + j)) 10817937Smckusick continue; 10917937Smckusick setbit(newcg->cg_free, i + j); 11017937Smckusick frags++; 11117937Smckusick } 11217937Smckusick if (frags == sblock.fs_frag) { 11317937Smckusick newcg->cg_cs.cs_nbfree++; 11417937Smckusick j = cbtocylno(&sblock, i); 11517937Smckusick newcg->cg_btot[j]++; 11617937Smckusick newcg->cg_b[j][cbtorpos(&sblock, i)]++; 11717937Smckusick } else if (frags > 0) { 11817937Smckusick newcg->cg_cs.cs_nffree += frags; 11917937Smckusick blk = blkmap(&sblock, newcg->cg_free, i); 12017937Smckusick fragacct(&sblock, blk, newcg->cg_frsum, 1); 12117937Smckusick } 12216267Smckusick } 12317937Smckusick for (frags = d; d < dmax; d++) { 12417937Smckusick if (getbmap(d)) 12517937Smckusick continue; 12617937Smckusick setbit(newcg->cg_free, d - dbase); 12717937Smckusick newcg->cg_cs.cs_nffree++; 12816267Smckusick } 12917937Smckusick if (frags != d) { 13017937Smckusick blk = blkmap(&sblock, newcg->cg_free, (frags - dbase)); 13117937Smckusick fragacct(&sblock, blk, newcg->cg_frsum, 1); 13216267Smckusick } 13317937Smckusick cstotal.cs_nffree += newcg->cg_cs.cs_nffree; 13417937Smckusick cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree; 13517937Smckusick cstotal.cs_nifree += newcg->cg_cs.cs_nifree; 13617937Smckusick cstotal.cs_ndir += newcg->cg_cs.cs_ndir; 13717937Smckusick if (bcmp(newcg->cg_iused, cgrp.cg_iused, mapsize) != 0 && 13817937Smckusick dofix(&idesc, "BLK(S) MISSING IN BIT MAPS")) { 13917937Smckusick bcopy(newcg->cg_iused, cgrp.cg_iused, mapsize); 14017937Smckusick cgdirty(); 14116267Smckusick } 14217937Smckusick if (bcmp((char *)newcg, (char *)&cgrp, sumsize) != 0 && 14317937Smckusick dofix(&idesc, "SUMMARY INFORMATION BAD")) { 14417937Smckusick bcopy((char *)newcg, (char *)&cgrp, sumsize); 14517937Smckusick cgdirty(); 14616267Smckusick } 14717937Smckusick cs = &sblock.fs_cs(&sblock, c); 14817937Smckusick if (bcmp((char *)&newcg->cg_cs, (char *)cs, sizeof *cs) != 0 && 14917937Smckusick dofix(&idesc, "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { 15017937Smckusick bcopy((char *)&newcg->cg_cs, (char *)cs, sizeof *cs); 15117937Smckusick sbdirty(); 15217937Smckusick } 15316267Smckusick } 15417937Smckusick if (bcmp((char *)&cstotal, (char *)&sblock.fs_cstotal, sizeof *cs) != 0 15517937Smckusick && dofix(&idesc, "FREE BLK COUNT(S) WRONG IN SUPERBLK")) { 15617937Smckusick bcopy((char *)&cstotal, (char *)&sblock.fs_cstotal, sizeof *cs); 15717937Smckusick sblock.fs_ronly = 0; 15817937Smckusick sblock.fs_fmod = 0; 15917937Smckusick sbdirty(); 16016267Smckusick } 16116267Smckusick } 162