xref: /csrg-svn/sbin/fsck/pass5.c (revision 18667)
116267Smckusick #ifndef lint
2*18667Smckusick static char version[] = "@(#)pass5.c	3.5 (Berkeley) 04/21/85";
316267Smckusick #endif
416267Smckusick 
516267Smckusick #include <sys/param.h>
616267Smckusick #include <sys/inode.h>
716267Smckusick #include <sys/fs.h>
816267Smckusick #include "fsck.h"
916267Smckusick 
1016267Smckusick pass5()
1116267Smckusick {
1217937Smckusick 	int c, blk, frags, sumsize, mapsize;
1317937Smckusick 	daddr_t dbase, dmax, d;
1417937Smckusick 	register long i, j;
1517937Smckusick 	struct csum *cs;
1617937Smckusick 	time_t now;
1717937Smckusick 	struct csum cstotal;
1817937Smckusick 	struct inodesc idesc;
1917937Smckusick 	char buf[MAXBSIZE];
2017937Smckusick 	register struct cg *newcg = (struct cg *)buf;
2116267Smckusick 
2217937Smckusick 	bzero((char *)newcg, sblock.fs_cgsize);
2317937Smckusick 	newcg->cg_magic = CG_MAGIC;
2417937Smckusick 	bzero((char *)&idesc, sizeof(struct inodesc));
2517937Smckusick 	idesc.id_type = ADDR;
2617937Smckusick 	bzero((char *)&cstotal, sizeof(struct csum));
2717937Smckusick 	sumsize = cgrp.cg_iused - (char *)(&cgrp);
28*18667Smckusick 	mapsize = &cgrp.cg_free[howmany(sblock.fs_fpg, NBBY)] -
29*18667Smckusick 		(u_char *)cgrp.cg_iused;
3017937Smckusick 	(void)time(&now);
3116267Smckusick 	for (c = 0; c < sblock.fs_ncg; c++) {
3216267Smckusick 		if (getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize) == 0)
3316267Smckusick 			continue;
3417937Smckusick 		if (cgrp.cg_magic != CG_MAGIC)
3516267Smckusick 			pfatal("CG %d: BAD MAGIC NUMBER\n", c);
3617937Smckusick 		dbase = cgbase(&sblock, c);
3717937Smckusick 		dmax = dbase + sblock.fs_fpg;
3817937Smckusick 		if (dmax > sblock.fs_size)
3917937Smckusick 			dmax = sblock.fs_size;
4017937Smckusick 		if (now > cgrp.cg_time)
4117937Smckusick 			newcg->cg_time = cgrp.cg_time;
4217937Smckusick 		else
4317937Smckusick 			newcg->cg_time = now;
4417937Smckusick 		newcg->cg_cgx = c;
4517937Smckusick 		if (c == sblock.fs_ncg - 1)
4617937Smckusick 			newcg->cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
4717937Smckusick 		else
4817937Smckusick 			newcg->cg_ncyl = sblock.fs_cpg;
4917937Smckusick 		newcg->cg_niblk = sblock.fs_ipg;
5017937Smckusick 		newcg->cg_ndblk = dmax - dbase;
5117937Smckusick 		newcg->cg_cs.cs_ndir = 0;
5217937Smckusick 		newcg->cg_cs.cs_nffree = 0;
5317937Smckusick 		newcg->cg_cs.cs_nbfree = 0;
5417937Smckusick 		newcg->cg_cs.cs_nifree = sblock.fs_ipg;
5517937Smckusick 		if (cgrp.cg_rotor < newcg->cg_ndblk)
5617937Smckusick 			newcg->cg_rotor = cgrp.cg_rotor;
5717937Smckusick 		else
5817937Smckusick 			newcg->cg_rotor = 0;
5917937Smckusick 		if (cgrp.cg_frotor < newcg->cg_ndblk)
6017937Smckusick 			newcg->cg_frotor = cgrp.cg_frotor;
6117937Smckusick 		else
6217937Smckusick 			newcg->cg_frotor = 0;
6317937Smckusick 		if (cgrp.cg_irotor < newcg->cg_niblk)
6417937Smckusick 			newcg->cg_irotor = cgrp.cg_irotor;
6517937Smckusick 		else
6617937Smckusick 			newcg->cg_irotor = 0;
6717937Smckusick 		bzero((char *)newcg->cg_frsum, sizeof newcg->cg_frsum);
6817937Smckusick 		bzero((char *)newcg->cg_btot, sizeof newcg->cg_btot);
6917937Smckusick 		bzero((char *)newcg->cg_b, sizeof newcg->cg_b);
7017937Smckusick 		bzero((char *)newcg->cg_free, howmany(sblock.fs_fpg, NBBY));
7117937Smckusick 		bzero((char *)newcg->cg_iused, howmany(sblock.fs_ipg, NBBY));
7217937Smckusick 		j = sblock.fs_ipg * c;
7317937Smckusick 		for (i = 0; i < sblock.fs_ipg; j++, i++) {
7417937Smckusick 			switch (statemap[j]) {
7517937Smckusick 
7617937Smckusick 			case USTATE:
7717937Smckusick 				break;
7817937Smckusick 
7917937Smckusick 			case DSTATE:
8017937Smckusick 			case DCLEAR:
8117937Smckusick 			case DFOUND:
8217937Smckusick 				newcg->cg_cs.cs_ndir++;
8317937Smckusick 				/* fall through */
8417937Smckusick 
8517937Smckusick 			case FSTATE:
8617937Smckusick 			case FCLEAR:
8717937Smckusick 				newcg->cg_cs.cs_nifree--;
8817937Smckusick 				setbit(newcg->cg_iused, i);
8917937Smckusick 				break;
9017937Smckusick 			}
9116267Smckusick 		}
9217937Smckusick 		if (c == 0)
9317937Smckusick 			for (i = 0; i < ROOTINO; i++) {
9417937Smckusick 				setbit(newcg->cg_iused, i);
9517937Smckusick 				newcg->cg_cs.cs_nifree--;
9616267Smckusick 			}
9717937Smckusick 		for (i = 0, d = dbase;
9817937Smckusick 		     d <= dmax - sblock.fs_frag;
9917937Smckusick 		     d += sblock.fs_frag, i += sblock.fs_frag) {
10017937Smckusick 			frags = 0;
10117937Smckusick 			for (j = 0; j < sblock.fs_frag; j++) {
10217937Smckusick 				if (getbmap(d + j))
10317937Smckusick 					continue;
10417937Smckusick 				setbit(newcg->cg_free, i + j);
10517937Smckusick 				frags++;
10617937Smckusick 			}
10717937Smckusick 			if (frags == sblock.fs_frag) {
10817937Smckusick 				newcg->cg_cs.cs_nbfree++;
10917937Smckusick 				j = cbtocylno(&sblock, i);
11017937Smckusick 				newcg->cg_btot[j]++;
11117937Smckusick 				newcg->cg_b[j][cbtorpos(&sblock, i)]++;
11217937Smckusick 			} else if (frags > 0) {
11317937Smckusick 				newcg->cg_cs.cs_nffree += frags;
11417937Smckusick 				blk = blkmap(&sblock, newcg->cg_free, i);
11517937Smckusick 				fragacct(&sblock, blk, newcg->cg_frsum, 1);
11617937Smckusick 			}
11716267Smckusick 		}
11817937Smckusick 		for (frags = d; d < dmax; d++) {
11917937Smckusick 			if (getbmap(d))
12017937Smckusick 				continue;
12117937Smckusick 			setbit(newcg->cg_free, d - dbase);
12217937Smckusick 			newcg->cg_cs.cs_nffree++;
12316267Smckusick 		}
12417937Smckusick 		if (frags != d) {
12517937Smckusick 			blk = blkmap(&sblock, newcg->cg_free, (frags - dbase));
12617937Smckusick 			fragacct(&sblock, blk, newcg->cg_frsum, 1);
12716267Smckusick 		}
12817937Smckusick 		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
12917937Smckusick 		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
13017937Smckusick 		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
13117937Smckusick 		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
13217937Smckusick 		if (bcmp(newcg->cg_iused, cgrp.cg_iused, mapsize) != 0 &&
13317937Smckusick 		    dofix(&idesc, "BLK(S) MISSING IN BIT MAPS")) {
13417937Smckusick 			bcopy(newcg->cg_iused, cgrp.cg_iused, mapsize);
13517937Smckusick 			cgdirty();
13616267Smckusick 		}
13717937Smckusick 		if (bcmp((char *)newcg, (char *)&cgrp, sumsize) != 0 &&
13817937Smckusick 		    dofix(&idesc, "SUMMARY INFORMATION BAD")) {
13917937Smckusick 			bcopy((char *)newcg, (char *)&cgrp, sumsize);
14017937Smckusick 			cgdirty();
14116267Smckusick 		}
14217937Smckusick 		cs = &sblock.fs_cs(&sblock, c);
14317937Smckusick 		if (bcmp((char *)&newcg->cg_cs, (char *)cs, sizeof *cs) != 0 &&
14417937Smckusick 		    dofix(&idesc, "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
14517937Smckusick 			bcopy((char *)&newcg->cg_cs, (char *)cs, sizeof *cs);
14617937Smckusick 			sbdirty();
14717937Smckusick 		}
14816267Smckusick 	}
14917937Smckusick 	if (bcmp((char *)&cstotal, (char *)&sblock.fs_cstotal, sizeof *cs) != 0
15017937Smckusick 	    && dofix(&idesc, "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
15117937Smckusick 		bcopy((char *)&cstotal, (char *)&sblock.fs_cstotal, sizeof *cs);
15217937Smckusick 		sblock.fs_ronly = 0;
15317937Smckusick 		sblock.fs_fmod = 0;
15417937Smckusick 		sbdirty();
15516267Smckusick 	}
15616267Smckusick }
157