xref: /csrg-svn/sbin/fsck/pass5.c (revision 42702)
122053Sdist /*
239976Smckusick  * Copyright (c) 1980, 1986 The Regents of the University of California.
339976Smckusick  * All rights reserved.
439976Smckusick  *
5*42702Sbostic  * %sccs.include.redist.c%
622053Sdist  */
722053Sdist 
816267Smckusick #ifndef lint
9*42702Sbostic static char sccsid[] = "@(#)pass5.c	5.12 (Berkeley) 06/01/90";
1039976Smckusick #endif /* not lint */
1116267Smckusick 
1216267Smckusick #include <sys/param.h>
1339383Smckusick #include <ufs/dinode.h>
1438337Smckusick #include <ufs/fs.h>
1516267Smckusick #include "fsck.h"
1616267Smckusick 
1716267Smckusick pass5()
1816267Smckusick {
1934141Smckusick 	int c, blk, frags, basesize, sumsize, mapsize, savednrpos;
2034223Smckusick 	register struct fs *fs = &sblock;
2134223Smckusick 	register struct cg *cg = &cgrp;
2234141Smckusick 	daddr_t dbase, dmax;
2334141Smckusick 	register daddr_t d;
2417937Smckusick 	register long i, j;
2517937Smckusick 	struct csum *cs;
2617937Smckusick 	time_t now;
2717937Smckusick 	struct csum cstotal;
2837099Smckusick 	struct inodesc idesc[3];
2917937Smckusick 	char buf[MAXBSIZE];
3017937Smckusick 	register struct cg *newcg = (struct cg *)buf;
3134141Smckusick 	struct ocg *ocg = (struct ocg *)buf;
3216267Smckusick 
3339973Smckusick 	bzero((char *)newcg, (int)fs->fs_cgsize);
3434223Smckusick 	newcg->cg_niblk = fs->fs_ipg;
3539973Smckusick 	switch ((int)fs->fs_postblformat) {
3634141Smckusick 
3734141Smckusick 	case FS_42POSTBLFMT:
3834141Smckusick 		basesize = (char *)(&ocg->cg_btot[0]) - (char *)(&ocg->cg_link);
3934141Smckusick 		sumsize = &ocg->cg_iused[0] - (char *)(&ocg->cg_btot[0]);
4034223Smckusick 		mapsize = &ocg->cg_free[howmany(fs->fs_fpg, NBBY)] -
4134141Smckusick 			(u_char *)&ocg->cg_iused[0];
4234141Smckusick 		ocg->cg_magic = CG_MAGIC;
4334223Smckusick 		savednrpos = fs->fs_nrpos;
4434223Smckusick 		fs->fs_nrpos = 8;
4534141Smckusick 		break;
4634141Smckusick 
4734141Smckusick 	case FS_DYNAMICPOSTBLFMT:
4834141Smckusick 		newcg->cg_btotoff =
4934141Smckusick 		 	&newcg->cg_space[0] - (u_char *)(&newcg->cg_link);
5034141Smckusick 		newcg->cg_boff =
5134223Smckusick 			newcg->cg_btotoff + fs->fs_cpg * sizeof(long);
5234141Smckusick 		newcg->cg_iusedoff = newcg->cg_boff +
5334223Smckusick 			fs->fs_cpg * fs->fs_nrpos * sizeof(short);
5434141Smckusick 		newcg->cg_freeoff =
5534223Smckusick 			newcg->cg_iusedoff + howmany(fs->fs_ipg, NBBY);
5634141Smckusick 		newcg->cg_nextfreeoff = newcg->cg_freeoff +
5734223Smckusick 			howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs),
5834141Smckusick 				NBBY);
5934141Smckusick 		newcg->cg_magic = CG_MAGIC;
6034141Smckusick 		basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_link);
6134141Smckusick 		sumsize = newcg->cg_iusedoff - newcg->cg_btotoff;
6234141Smckusick 		mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
6334141Smckusick 		break;
6434141Smckusick 
6534141Smckusick 	default:
6634141Smckusick 		errexit("UNKNOWN ROTATIONAL TABLE FORMAT %d\n",
6734223Smckusick 			fs->fs_postblformat);
6834141Smckusick 	}
6937099Smckusick 	bzero((char *)&idesc[0], sizeof idesc);
7037099Smckusick 	for (i = 0; i < 3; i++)
7137099Smckusick 		idesc[i].id_type = ADDR;
7217937Smckusick 	bzero((char *)&cstotal, sizeof(struct csum));
7317937Smckusick 	(void)time(&now);
7440018Smckusick 	j = blknum(fs, fs->fs_size + fs->fs_frag - 1);
7540018Smckusick 	for (i = fs->fs_size; i < j; i++)
7634141Smckusick 		setbmap(i);
7734223Smckusick 	for (c = 0; c < fs->fs_ncg; c++) {
7834223Smckusick 		getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize);
7934223Smckusick 		if (!cg_chkmagic(cg))
8016267Smckusick 			pfatal("CG %d: BAD MAGIC NUMBER\n", c);
8134223Smckusick 		dbase = cgbase(fs, c);
8234223Smckusick 		dmax = dbase + fs->fs_fpg;
8334223Smckusick 		if (dmax > fs->fs_size)
8434223Smckusick 			dmax = fs->fs_size;
8534223Smckusick 		if (now > cg->cg_time)
8634223Smckusick 			newcg->cg_time = cg->cg_time;
8717937Smckusick 		else
8817937Smckusick 			newcg->cg_time = now;
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);
11234141Smckusick 		bzero((char *)&cg_blktot(newcg)[0], sumsize + mapsize);
11334223Smckusick 		if (fs->fs_postblformat == FS_42POSTBLFMT)
11434141Smckusick 			ocg->cg_magic = CG_MAGIC;
11534223Smckusick 		j = fs->fs_ipg * c;
11634223Smckusick 		for (i = 0; i < fs->fs_ipg; j++, i++) {
11717937Smckusick 			switch (statemap[j]) {
11817937Smckusick 
11917937Smckusick 			case USTATE:
12017937Smckusick 				break;
12117937Smckusick 
12217937Smckusick 			case DSTATE:
12317937Smckusick 			case DCLEAR:
12417937Smckusick 			case DFOUND:
12517937Smckusick 				newcg->cg_cs.cs_ndir++;
12617937Smckusick 				/* fall through */
12717937Smckusick 
12817937Smckusick 			case FSTATE:
12917937Smckusick 			case FCLEAR:
13017937Smckusick 				newcg->cg_cs.cs_nifree--;
13134141Smckusick 				setbit(cg_inosused(newcg), i);
13217937Smckusick 				break;
13326480Smckusick 
13426480Smckusick 			default:
13526480Smckusick 				if (j < ROOTINO)
13626480Smckusick 					break;
13726480Smckusick 				errexit("BAD STATE %d FOR INODE I=%d",
13826480Smckusick 				    statemap[j], j);
13917937Smckusick 			}
14016267Smckusick 		}
14117937Smckusick 		if (c == 0)
14217937Smckusick 			for (i = 0; i < ROOTINO; i++) {
14334141Smckusick 				setbit(cg_inosused(newcg), i);
14417937Smckusick 				newcg->cg_cs.cs_nifree--;
14516267Smckusick 			}
14617937Smckusick 		for (i = 0, d = dbase;
14734141Smckusick 		     d < dmax;
14834223Smckusick 		     d += fs->fs_frag, i += fs->fs_frag) {
14917937Smckusick 			frags = 0;
15034223Smckusick 			for (j = 0; j < fs->fs_frag; j++) {
15139973Smckusick 				if (testbmap(d + j))
15217937Smckusick 					continue;
15334141Smckusick 				setbit(cg_blksfree(newcg), i + j);
15417937Smckusick 				frags++;
15517937Smckusick 			}
15634223Smckusick 			if (frags == fs->fs_frag) {
15717937Smckusick 				newcg->cg_cs.cs_nbfree++;
15834223Smckusick 				j = cbtocylno(fs, i);
15934141Smckusick 				cg_blktot(newcg)[j]++;
16034223Smckusick 				cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++;
16117937Smckusick 			} else if (frags > 0) {
16217937Smckusick 				newcg->cg_cs.cs_nffree += frags;
16334223Smckusick 				blk = blkmap(fs, cg_blksfree(newcg), i);
16434223Smckusick 				fragacct(fs, blk, newcg->cg_frsum, 1);
16517937Smckusick 			}
16616267Smckusick 		}
16717937Smckusick 		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
16817937Smckusick 		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
16917937Smckusick 		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
17017937Smckusick 		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
17134223Smckusick 		cs = &fs->fs_cs(fs, c);
17234141Smckusick 		if (bcmp((char *)&newcg->cg_cs, (char *)cs, sizeof *cs) != 0 &&
17337099Smckusick 		    dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
17434141Smckusick 			bcopy((char *)&newcg->cg_cs, (char *)cs, sizeof *cs);
17534141Smckusick 			sbdirty();
17634141Smckusick 		}
17734141Smckusick 		if (cvtflag) {
17839973Smckusick 			bcopy((char *)newcg, (char *)cg, (int)fs->fs_cgsize);
17934141Smckusick 			cgdirty();
18034141Smckusick 			continue;
18134141Smckusick 		}
18234141Smckusick 		if (bcmp(cg_inosused(newcg),
18334223Smckusick 			 cg_inosused(cg), mapsize) != 0 &&
18437099Smckusick 		    dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) {
18534223Smckusick 			bcopy(cg_inosused(newcg), cg_inosused(cg), mapsize);
18617937Smckusick 			cgdirty();
18716267Smckusick 		}
18834223Smckusick 		if ((bcmp((char *)newcg, (char *)cg, basesize) != 0 ||
18934141Smckusick 		     bcmp((char *)&cg_blktot(newcg)[0],
19034223Smckusick 			  (char *)&cg_blktot(cg)[0], sumsize) != 0) &&
19137099Smckusick 		    dofix(&idesc[2], "SUMMARY INFORMATION BAD")) {
19234223Smckusick 			bcopy((char *)newcg, (char *)cg, basesize);
19334141Smckusick 			bcopy((char *)&cg_blktot(newcg)[0],
19434223Smckusick 			      (char *)&cg_blktot(cg)[0], sumsize);
19517937Smckusick 			cgdirty();
19616267Smckusick 		}
19716267Smckusick 	}
19834223Smckusick 	if (fs->fs_postblformat == FS_42POSTBLFMT)
19934223Smckusick 		fs->fs_nrpos = savednrpos;
20034223Smckusick 	if (bcmp((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs) != 0
20137099Smckusick 	    && dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
20234223Smckusick 		bcopy((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs);
20334223Smckusick 		fs->fs_ronly = 0;
20434223Smckusick 		fs->fs_fmod = 0;
20517937Smckusick 		sbdirty();
20616267Smckusick 	}
20716267Smckusick }
208