xref: /csrg-svn/sbin/fsck/pass5.c (revision 53703)
122053Sdist /*
239976Smckusick  * Copyright (c) 1980, 1986 The Regents of the University of California.
339976Smckusick  * All rights reserved.
439976Smckusick  *
542702Sbostic  * %sccs.include.redist.c%
622053Sdist  */
722053Sdist 
816267Smckusick #ifndef lint
9*53703Smckusick static char sccsid[] = "@(#)pass5.c	5.16 (Berkeley) 05/27/92";
1039976Smckusick #endif /* not lint */
1116267Smckusick 
1216267Smckusick #include <sys/param.h>
13*53703Smckusick #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 	time_t now;
2917937Smckusick 	struct csum cstotal;
3037099Smckusick 	struct inodesc idesc[3];
3117937Smckusick 	char buf[MAXBSIZE];
3217937Smckusick 	register struct cg *newcg = (struct cg *)buf;
3334141Smckusick 	struct ocg *ocg = (struct ocg *)buf;
3416267Smckusick 
3544934Smckusick 	bzero((char *)newcg, (size_t)fs->fs_cgsize);
3634223Smckusick 	newcg->cg_niblk = fs->fs_ipg;
3739973Smckusick 	switch ((int)fs->fs_postblformat) {
3834141Smckusick 
3934141Smckusick 	case FS_42POSTBLFMT:
4034141Smckusick 		basesize = (char *)(&ocg->cg_btot[0]) - (char *)(&ocg->cg_link);
4134141Smckusick 		sumsize = &ocg->cg_iused[0] - (char *)(&ocg->cg_btot[0]);
4234223Smckusick 		mapsize = &ocg->cg_free[howmany(fs->fs_fpg, NBBY)] -
4334141Smckusick 			(u_char *)&ocg->cg_iused[0];
4434141Smckusick 		ocg->cg_magic = CG_MAGIC;
4534223Smckusick 		savednrpos = fs->fs_nrpos;
4634223Smckusick 		fs->fs_nrpos = 8;
4734141Smckusick 		break;
4834141Smckusick 
4934141Smckusick 	case FS_DYNAMICPOSTBLFMT:
5034141Smckusick 		newcg->cg_btotoff =
5134141Smckusick 		 	&newcg->cg_space[0] - (u_char *)(&newcg->cg_link);
5234141Smckusick 		newcg->cg_boff =
5334223Smckusick 			newcg->cg_btotoff + fs->fs_cpg * sizeof(long);
5434141Smckusick 		newcg->cg_iusedoff = newcg->cg_boff +
5534223Smckusick 			fs->fs_cpg * fs->fs_nrpos * sizeof(short);
5634141Smckusick 		newcg->cg_freeoff =
5734223Smckusick 			newcg->cg_iusedoff + howmany(fs->fs_ipg, NBBY);
5834141Smckusick 		newcg->cg_nextfreeoff = newcg->cg_freeoff +
5934223Smckusick 			howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs),
6034141Smckusick 				NBBY);
6134141Smckusick 		newcg->cg_magic = CG_MAGIC;
6234141Smckusick 		basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_link);
6334141Smckusick 		sumsize = newcg->cg_iusedoff - newcg->cg_btotoff;
6434141Smckusick 		mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
6534141Smckusick 		break;
6634141Smckusick 
6734141Smckusick 	default:
6834141Smckusick 		errexit("UNKNOWN ROTATIONAL TABLE FORMAT %d\n",
6934223Smckusick 			fs->fs_postblformat);
7034141Smckusick 	}
7137099Smckusick 	bzero((char *)&idesc[0], sizeof idesc);
7237099Smckusick 	for (i = 0; i < 3; i++)
7337099Smckusick 		idesc[i].id_type = ADDR;
7417937Smckusick 	bzero((char *)&cstotal, sizeof(struct csum));
7517937Smckusick 	(void)time(&now);
7640018Smckusick 	j = blknum(fs, fs->fs_size + fs->fs_frag - 1);
7740018Smckusick 	for (i = fs->fs_size; i < j; i++)
7834141Smckusick 		setbmap(i);
7934223Smckusick 	for (c = 0; c < fs->fs_ncg; c++) {
8034223Smckusick 		getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize);
8134223Smckusick 		if (!cg_chkmagic(cg))
8216267Smckusick 			pfatal("CG %d: BAD MAGIC NUMBER\n", c);
8334223Smckusick 		dbase = cgbase(fs, c);
8434223Smckusick 		dmax = dbase + fs->fs_fpg;
8534223Smckusick 		if (dmax > fs->fs_size)
8634223Smckusick 			dmax = fs->fs_size;
8734223Smckusick 		if (now > cg->cg_time)
8834223Smckusick 			newcg->cg_time = cg->cg_time;
8917937Smckusick 		else
9017937Smckusick 			newcg->cg_time = now;
9117937Smckusick 		newcg->cg_cgx = c;
9234223Smckusick 		if (c == fs->fs_ncg - 1)
9334223Smckusick 			newcg->cg_ncyl = fs->fs_ncyl % fs->fs_cpg;
9417937Smckusick 		else
9534223Smckusick 			newcg->cg_ncyl = fs->fs_cpg;
9617937Smckusick 		newcg->cg_ndblk = dmax - dbase;
9717937Smckusick 		newcg->cg_cs.cs_ndir = 0;
9817937Smckusick 		newcg->cg_cs.cs_nffree = 0;
9917937Smckusick 		newcg->cg_cs.cs_nbfree = 0;
10034223Smckusick 		newcg->cg_cs.cs_nifree = fs->fs_ipg;
10134223Smckusick 		if (cg->cg_rotor < newcg->cg_ndblk)
10234223Smckusick 			newcg->cg_rotor = cg->cg_rotor;
10317937Smckusick 		else
10417937Smckusick 			newcg->cg_rotor = 0;
10534223Smckusick 		if (cg->cg_frotor < newcg->cg_ndblk)
10634223Smckusick 			newcg->cg_frotor = cg->cg_frotor;
10717937Smckusick 		else
10817937Smckusick 			newcg->cg_frotor = 0;
10934223Smckusick 		if (cg->cg_irotor < newcg->cg_niblk)
11034223Smckusick 			newcg->cg_irotor = cg->cg_irotor;
11117937Smckusick 		else
11217937Smckusick 			newcg->cg_irotor = 0;
11334141Smckusick 		bzero((char *)&newcg->cg_frsum[0], sizeof newcg->cg_frsum);
11444934Smckusick 		bzero((char *)&cg_blktot(newcg)[0],
11544934Smckusick 		      (size_t)(sumsize + mapsize));
11634223Smckusick 		if (fs->fs_postblformat == FS_42POSTBLFMT)
11734141Smckusick 			ocg->cg_magic = CG_MAGIC;
11834223Smckusick 		j = fs->fs_ipg * c;
11934223Smckusick 		for (i = 0; i < fs->fs_ipg; j++, i++) {
12017937Smckusick 			switch (statemap[j]) {
12117937Smckusick 
12217937Smckusick 			case USTATE:
12317937Smckusick 				break;
12417937Smckusick 
12517937Smckusick 			case DSTATE:
12617937Smckusick 			case DCLEAR:
12717937Smckusick 			case DFOUND:
12817937Smckusick 				newcg->cg_cs.cs_ndir++;
12917937Smckusick 				/* fall through */
13017937Smckusick 
13117937Smckusick 			case FSTATE:
13217937Smckusick 			case FCLEAR:
13317937Smckusick 				newcg->cg_cs.cs_nifree--;
13434141Smckusick 				setbit(cg_inosused(newcg), i);
13517937Smckusick 				break;
13626480Smckusick 
13726480Smckusick 			default:
13826480Smckusick 				if (j < ROOTINO)
13926480Smckusick 					break;
14026480Smckusick 				errexit("BAD STATE %d FOR INODE I=%d",
14126480Smckusick 				    statemap[j], j);
14217937Smckusick 			}
14316267Smckusick 		}
14417937Smckusick 		if (c == 0)
14517937Smckusick 			for (i = 0; i < ROOTINO; i++) {
14634141Smckusick 				setbit(cg_inosused(newcg), i);
14717937Smckusick 				newcg->cg_cs.cs_nifree--;
14816267Smckusick 			}
14917937Smckusick 		for (i = 0, d = dbase;
15034141Smckusick 		     d < dmax;
15134223Smckusick 		     d += fs->fs_frag, i += fs->fs_frag) {
15217937Smckusick 			frags = 0;
15334223Smckusick 			for (j = 0; j < fs->fs_frag; j++) {
15439973Smckusick 				if (testbmap(d + j))
15517937Smckusick 					continue;
15634141Smckusick 				setbit(cg_blksfree(newcg), i + j);
15717937Smckusick 				frags++;
15817937Smckusick 			}
15934223Smckusick 			if (frags == fs->fs_frag) {
16017937Smckusick 				newcg->cg_cs.cs_nbfree++;
16134223Smckusick 				j = cbtocylno(fs, i);
16234141Smckusick 				cg_blktot(newcg)[j]++;
16334223Smckusick 				cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++;
16417937Smckusick 			} else if (frags > 0) {
16517937Smckusick 				newcg->cg_cs.cs_nffree += frags;
16634223Smckusick 				blk = blkmap(fs, cg_blksfree(newcg), i);
16751637Sbostic 				ffs_fragacct(fs, blk, newcg->cg_frsum, 1);
16817937Smckusick 			}
16916267Smckusick 		}
17017937Smckusick 		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
17117937Smckusick 		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
17217937Smckusick 		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
17317937Smckusick 		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
17434223Smckusick 		cs = &fs->fs_cs(fs, c);
17534141Smckusick 		if (bcmp((char *)&newcg->cg_cs, (char *)cs, sizeof *cs) != 0 &&
17637099Smckusick 		    dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
17734141Smckusick 			bcopy((char *)&newcg->cg_cs, (char *)cs, sizeof *cs);
17834141Smckusick 			sbdirty();
17934141Smckusick 		}
18034141Smckusick 		if (cvtflag) {
18144934Smckusick 			bcopy((char *)newcg, (char *)cg, (size_t)fs->fs_cgsize);
18234141Smckusick 			cgdirty();
18334141Smckusick 			continue;
18434141Smckusick 		}
18534141Smckusick 		if (bcmp(cg_inosused(newcg),
18634223Smckusick 			 cg_inosused(cg), mapsize) != 0 &&
18737099Smckusick 		    dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) {
18844934Smckusick 			bcopy(cg_inosused(newcg), cg_inosused(cg),
18944934Smckusick 			      (size_t)mapsize);
19017937Smckusick 			cgdirty();
19116267Smckusick 		}
19234223Smckusick 		if ((bcmp((char *)newcg, (char *)cg, basesize) != 0 ||
19334141Smckusick 		     bcmp((char *)&cg_blktot(newcg)[0],
19434223Smckusick 			  (char *)&cg_blktot(cg)[0], sumsize) != 0) &&
19537099Smckusick 		    dofix(&idesc[2], "SUMMARY INFORMATION BAD")) {
19644934Smckusick 			bcopy((char *)newcg, (char *)cg, (size_t)basesize);
19734141Smckusick 			bcopy((char *)&cg_blktot(newcg)[0],
19844934Smckusick 			      (char *)&cg_blktot(cg)[0], (size_t)sumsize);
19917937Smckusick 			cgdirty();
20016267Smckusick 		}
20116267Smckusick 	}
20234223Smckusick 	if (fs->fs_postblformat == FS_42POSTBLFMT)
20334223Smckusick 		fs->fs_nrpos = savednrpos;
20434223Smckusick 	if (bcmp((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs) != 0
20537099Smckusick 	    && dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
20634223Smckusick 		bcopy((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs);
20734223Smckusick 		fs->fs_ronly = 0;
20834223Smckusick 		fs->fs_fmod = 0;
20917937Smckusick 		sbdirty();
21016267Smckusick 	}
21116267Smckusick }
212