xref: /csrg-svn/sbin/fsck/pass5.c (revision 21540)
116267Smckusick #ifndef lint
2*21540Smckusick static char version[] = "@(#)pass5.c	3.6 (Berkeley) 05/31/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);
2818667Smckusick 	mapsize = &cgrp.cg_free[howmany(sblock.fs_fpg, NBBY)] -
2918667Smckusick 		(u_char *)cgrp.cg_iused;
3017937Smckusick 	(void)time(&now);
3116267Smckusick 	for (c = 0; c < sblock.fs_ncg; c++) {
32*21540Smckusick 		getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
3317937Smckusick 		if (cgrp.cg_magic != CG_MAGIC)
3416267Smckusick 			pfatal("CG %d: BAD MAGIC NUMBER\n", c);
3517937Smckusick 		dbase = cgbase(&sblock, c);
3617937Smckusick 		dmax = dbase + sblock.fs_fpg;
3717937Smckusick 		if (dmax > sblock.fs_size)
3817937Smckusick 			dmax = sblock.fs_size;
3917937Smckusick 		if (now > cgrp.cg_time)
4017937Smckusick 			newcg->cg_time = cgrp.cg_time;
4117937Smckusick 		else
4217937Smckusick 			newcg->cg_time = now;
4317937Smckusick 		newcg->cg_cgx = c;
4417937Smckusick 		if (c == sblock.fs_ncg - 1)
4517937Smckusick 			newcg->cg_ncyl = sblock.fs_ncyl % sblock.fs_cpg;
4617937Smckusick 		else
4717937Smckusick 			newcg->cg_ncyl = sblock.fs_cpg;
4817937Smckusick 		newcg->cg_niblk = sblock.fs_ipg;
4917937Smckusick 		newcg->cg_ndblk = dmax - dbase;
5017937Smckusick 		newcg->cg_cs.cs_ndir = 0;
5117937Smckusick 		newcg->cg_cs.cs_nffree = 0;
5217937Smckusick 		newcg->cg_cs.cs_nbfree = 0;
5317937Smckusick 		newcg->cg_cs.cs_nifree = sblock.fs_ipg;
5417937Smckusick 		if (cgrp.cg_rotor < newcg->cg_ndblk)
5517937Smckusick 			newcg->cg_rotor = cgrp.cg_rotor;
5617937Smckusick 		else
5717937Smckusick 			newcg->cg_rotor = 0;
5817937Smckusick 		if (cgrp.cg_frotor < newcg->cg_ndblk)
5917937Smckusick 			newcg->cg_frotor = cgrp.cg_frotor;
6017937Smckusick 		else
6117937Smckusick 			newcg->cg_frotor = 0;
6217937Smckusick 		if (cgrp.cg_irotor < newcg->cg_niblk)
6317937Smckusick 			newcg->cg_irotor = cgrp.cg_irotor;
6417937Smckusick 		else
6517937Smckusick 			newcg->cg_irotor = 0;
6617937Smckusick 		bzero((char *)newcg->cg_frsum, sizeof newcg->cg_frsum);
6717937Smckusick 		bzero((char *)newcg->cg_btot, sizeof newcg->cg_btot);
6817937Smckusick 		bzero((char *)newcg->cg_b, sizeof newcg->cg_b);
6917937Smckusick 		bzero((char *)newcg->cg_free, howmany(sblock.fs_fpg, NBBY));
7017937Smckusick 		bzero((char *)newcg->cg_iused, howmany(sblock.fs_ipg, NBBY));
7117937Smckusick 		j = sblock.fs_ipg * c;
7217937Smckusick 		for (i = 0; i < sblock.fs_ipg; j++, i++) {
7317937Smckusick 			switch (statemap[j]) {
7417937Smckusick 
7517937Smckusick 			case USTATE:
7617937Smckusick 				break;
7717937Smckusick 
7817937Smckusick 			case DSTATE:
7917937Smckusick 			case DCLEAR:
8017937Smckusick 			case DFOUND:
8117937Smckusick 				newcg->cg_cs.cs_ndir++;
8217937Smckusick 				/* fall through */
8317937Smckusick 
8417937Smckusick 			case FSTATE:
8517937Smckusick 			case FCLEAR:
8617937Smckusick 				newcg->cg_cs.cs_nifree--;
8717937Smckusick 				setbit(newcg->cg_iused, i);
8817937Smckusick 				break;
8917937Smckusick 			}
9016267Smckusick 		}
9117937Smckusick 		if (c == 0)
9217937Smckusick 			for (i = 0; i < ROOTINO; i++) {
9317937Smckusick 				setbit(newcg->cg_iused, i);
9417937Smckusick 				newcg->cg_cs.cs_nifree--;
9516267Smckusick 			}
9617937Smckusick 		for (i = 0, d = dbase;
9717937Smckusick 		     d <= dmax - sblock.fs_frag;
9817937Smckusick 		     d += sblock.fs_frag, i += sblock.fs_frag) {
9917937Smckusick 			frags = 0;
10017937Smckusick 			for (j = 0; j < sblock.fs_frag; j++) {
10117937Smckusick 				if (getbmap(d + j))
10217937Smckusick 					continue;
10317937Smckusick 				setbit(newcg->cg_free, i + j);
10417937Smckusick 				frags++;
10517937Smckusick 			}
10617937Smckusick 			if (frags == sblock.fs_frag) {
10717937Smckusick 				newcg->cg_cs.cs_nbfree++;
10817937Smckusick 				j = cbtocylno(&sblock, i);
10917937Smckusick 				newcg->cg_btot[j]++;
11017937Smckusick 				newcg->cg_b[j][cbtorpos(&sblock, i)]++;
11117937Smckusick 			} else if (frags > 0) {
11217937Smckusick 				newcg->cg_cs.cs_nffree += frags;
11317937Smckusick 				blk = blkmap(&sblock, newcg->cg_free, i);
11417937Smckusick 				fragacct(&sblock, blk, newcg->cg_frsum, 1);
11517937Smckusick 			}
11616267Smckusick 		}
11717937Smckusick 		for (frags = d; d < dmax; d++) {
11817937Smckusick 			if (getbmap(d))
11917937Smckusick 				continue;
12017937Smckusick 			setbit(newcg->cg_free, d - dbase);
12117937Smckusick 			newcg->cg_cs.cs_nffree++;
12216267Smckusick 		}
12317937Smckusick 		if (frags != d) {
12417937Smckusick 			blk = blkmap(&sblock, newcg->cg_free, (frags - dbase));
12517937Smckusick 			fragacct(&sblock, blk, newcg->cg_frsum, 1);
12616267Smckusick 		}
12717937Smckusick 		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
12817937Smckusick 		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
12917937Smckusick 		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
13017937Smckusick 		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
13117937Smckusick 		if (bcmp(newcg->cg_iused, cgrp.cg_iused, mapsize) != 0 &&
13217937Smckusick 		    dofix(&idesc, "BLK(S) MISSING IN BIT MAPS")) {
13317937Smckusick 			bcopy(newcg->cg_iused, cgrp.cg_iused, mapsize);
13417937Smckusick 			cgdirty();
13516267Smckusick 		}
13617937Smckusick 		if (bcmp((char *)newcg, (char *)&cgrp, sumsize) != 0 &&
13717937Smckusick 		    dofix(&idesc, "SUMMARY INFORMATION BAD")) {
13817937Smckusick 			bcopy((char *)newcg, (char *)&cgrp, sumsize);
13917937Smckusick 			cgdirty();
14016267Smckusick 		}
14117937Smckusick 		cs = &sblock.fs_cs(&sblock, c);
14217937Smckusick 		if (bcmp((char *)&newcg->cg_cs, (char *)cs, sizeof *cs) != 0 &&
14317937Smckusick 		    dofix(&idesc, "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
14417937Smckusick 			bcopy((char *)&newcg->cg_cs, (char *)cs, sizeof *cs);
14517937Smckusick 			sbdirty();
14617937Smckusick 		}
14716267Smckusick 	}
14817937Smckusick 	if (bcmp((char *)&cstotal, (char *)&sblock.fs_cstotal, sizeof *cs) != 0
14917937Smckusick 	    && dofix(&idesc, "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
15017937Smckusick 		bcopy((char *)&cstotal, (char *)&sblock.fs_cstotal, sizeof *cs);
15117937Smckusick 		sblock.fs_ronly = 0;
15217937Smckusick 		sblock.fs_fmod = 0;
15317937Smckusick 		sbdirty();
15416267Smckusick 	}
15516267Smckusick }
156