xref: /csrg-svn/sbin/fsck/pass5.c (revision 37099)
122053Sdist /*
222053Sdist  * Copyright (c) 1980 Regents of the University of California.
322053Sdist  * All rights reserved.  The Berkeley software License Agreement
422053Sdist  * specifies the terms and conditions for redistribution.
522053Sdist  */
622053Sdist 
716267Smckusick #ifndef lint
8*37099Smckusick static char sccsid[] = "@(#)pass5.c	5.6 (Berkeley) 03/07/89";
922053Sdist #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 {
1834141Smckusick 	int c, blk, frags, basesize, sumsize, mapsize, savednrpos;
1934223Smckusick 	register struct fs *fs = &sblock;
2034223Smckusick 	register struct cg *cg = &cgrp;
2134141Smckusick 	daddr_t dbase, dmax;
2234141Smckusick 	register daddr_t d;
2317937Smckusick 	register long i, j;
2417937Smckusick 	struct csum *cs;
2517937Smckusick 	time_t now;
2617937Smckusick 	struct csum cstotal;
27*37099Smckusick 	struct inodesc idesc[3];
2817937Smckusick 	char buf[MAXBSIZE];
2917937Smckusick 	register struct cg *newcg = (struct cg *)buf;
3034141Smckusick 	struct ocg *ocg = (struct ocg *)buf;
3116267Smckusick 
3234223Smckusick 	bzero((char *)newcg, fs->fs_cgsize);
3334223Smckusick 	newcg->cg_niblk = fs->fs_ipg;
3434223Smckusick 	switch (fs->fs_postblformat) {
3534141Smckusick 
3634141Smckusick 	case FS_42POSTBLFMT:
3734141Smckusick 		basesize = (char *)(&ocg->cg_btot[0]) - (char *)(&ocg->cg_link);
3834141Smckusick 		sumsize = &ocg->cg_iused[0] - (char *)(&ocg->cg_btot[0]);
3934223Smckusick 		mapsize = &ocg->cg_free[howmany(fs->fs_fpg, NBBY)] -
4034141Smckusick 			(u_char *)&ocg->cg_iused[0];
4134141Smckusick 		ocg->cg_magic = CG_MAGIC;
4234223Smckusick 		savednrpos = fs->fs_nrpos;
4334223Smckusick 		fs->fs_nrpos = 8;
4434141Smckusick 		break;
4534141Smckusick 
4634141Smckusick 	case FS_DYNAMICPOSTBLFMT:
4734141Smckusick 		newcg->cg_btotoff =
4834141Smckusick 		 	&newcg->cg_space[0] - (u_char *)(&newcg->cg_link);
4934141Smckusick 		newcg->cg_boff =
5034223Smckusick 			newcg->cg_btotoff + fs->fs_cpg * sizeof(long);
5134141Smckusick 		newcg->cg_iusedoff = newcg->cg_boff +
5234223Smckusick 			fs->fs_cpg * fs->fs_nrpos * sizeof(short);
5334141Smckusick 		newcg->cg_freeoff =
5434223Smckusick 			newcg->cg_iusedoff + howmany(fs->fs_ipg, NBBY);
5534141Smckusick 		newcg->cg_nextfreeoff = newcg->cg_freeoff +
5634223Smckusick 			howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs),
5734141Smckusick 				NBBY);
5834141Smckusick 		newcg->cg_magic = CG_MAGIC;
5934141Smckusick 		basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_link);
6034141Smckusick 		sumsize = newcg->cg_iusedoff - newcg->cg_btotoff;
6134141Smckusick 		mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
6234141Smckusick 		break;
6334141Smckusick 
6434141Smckusick 	default:
6534141Smckusick 		errexit("UNKNOWN ROTATIONAL TABLE FORMAT %d\n",
6634223Smckusick 			fs->fs_postblformat);
6734141Smckusick 	}
68*37099Smckusick 	bzero((char *)&idesc[0], sizeof idesc);
69*37099Smckusick 	for (i = 0; i < 3; i++)
70*37099Smckusick 		idesc[i].id_type = ADDR;
7117937Smckusick 	bzero((char *)&cstotal, sizeof(struct csum));
7217937Smckusick 	(void)time(&now);
7334223Smckusick 	for (i = fs->fs_size; i < fragroundup(fs, fs->fs_size); i++)
7434141Smckusick 		setbmap(i);
7534223Smckusick 	for (c = 0; c < fs->fs_ncg; c++) {
7634223Smckusick 		getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize);
7734223Smckusick 		if (!cg_chkmagic(cg))
7816267Smckusick 			pfatal("CG %d: BAD MAGIC NUMBER\n", c);
7934223Smckusick 		dbase = cgbase(fs, c);
8034223Smckusick 		dmax = dbase + fs->fs_fpg;
8134223Smckusick 		if (dmax > fs->fs_size)
8234223Smckusick 			dmax = fs->fs_size;
8334223Smckusick 		if (now > cg->cg_time)
8434223Smckusick 			newcg->cg_time = cg->cg_time;
8517937Smckusick 		else
8617937Smckusick 			newcg->cg_time = now;
8717937Smckusick 		newcg->cg_cgx = c;
8834223Smckusick 		if (c == fs->fs_ncg - 1)
8934223Smckusick 			newcg->cg_ncyl = fs->fs_ncyl % fs->fs_cpg;
9017937Smckusick 		else
9134223Smckusick 			newcg->cg_ncyl = fs->fs_cpg;
9217937Smckusick 		newcg->cg_ndblk = dmax - dbase;
9317937Smckusick 		newcg->cg_cs.cs_ndir = 0;
9417937Smckusick 		newcg->cg_cs.cs_nffree = 0;
9517937Smckusick 		newcg->cg_cs.cs_nbfree = 0;
9634223Smckusick 		newcg->cg_cs.cs_nifree = fs->fs_ipg;
9734223Smckusick 		if (cg->cg_rotor < newcg->cg_ndblk)
9834223Smckusick 			newcg->cg_rotor = cg->cg_rotor;
9917937Smckusick 		else
10017937Smckusick 			newcg->cg_rotor = 0;
10134223Smckusick 		if (cg->cg_frotor < newcg->cg_ndblk)
10234223Smckusick 			newcg->cg_frotor = cg->cg_frotor;
10317937Smckusick 		else
10417937Smckusick 			newcg->cg_frotor = 0;
10534223Smckusick 		if (cg->cg_irotor < newcg->cg_niblk)
10634223Smckusick 			newcg->cg_irotor = cg->cg_irotor;
10717937Smckusick 		else
10817937Smckusick 			newcg->cg_irotor = 0;
10934141Smckusick 		bzero((char *)&newcg->cg_frsum[0], sizeof newcg->cg_frsum);
11034141Smckusick 		bzero((char *)&cg_blktot(newcg)[0], sumsize + mapsize);
11134223Smckusick 		if (fs->fs_postblformat == FS_42POSTBLFMT)
11234141Smckusick 			ocg->cg_magic = CG_MAGIC;
11334223Smckusick 		j = fs->fs_ipg * c;
11434223Smckusick 		for (i = 0; i < fs->fs_ipg; j++, i++) {
11517937Smckusick 			switch (statemap[j]) {
11617937Smckusick 
11717937Smckusick 			case USTATE:
11817937Smckusick 				break;
11917937Smckusick 
12017937Smckusick 			case DSTATE:
12117937Smckusick 			case DCLEAR:
12217937Smckusick 			case DFOUND:
12317937Smckusick 				newcg->cg_cs.cs_ndir++;
12417937Smckusick 				/* fall through */
12517937Smckusick 
12617937Smckusick 			case FSTATE:
12717937Smckusick 			case FCLEAR:
12817937Smckusick 				newcg->cg_cs.cs_nifree--;
12934141Smckusick 				setbit(cg_inosused(newcg), i);
13017937Smckusick 				break;
13126480Smckusick 
13226480Smckusick 			default:
13326480Smckusick 				if (j < ROOTINO)
13426480Smckusick 					break;
13526480Smckusick 				errexit("BAD STATE %d FOR INODE I=%d",
13626480Smckusick 				    statemap[j], j);
13717937Smckusick 			}
13816267Smckusick 		}
13917937Smckusick 		if (c == 0)
14017937Smckusick 			for (i = 0; i < ROOTINO; i++) {
14134141Smckusick 				setbit(cg_inosused(newcg), i);
14217937Smckusick 				newcg->cg_cs.cs_nifree--;
14316267Smckusick 			}
14417937Smckusick 		for (i = 0, d = dbase;
14534141Smckusick 		     d < dmax;
14634223Smckusick 		     d += fs->fs_frag, i += fs->fs_frag) {
14717937Smckusick 			frags = 0;
14834223Smckusick 			for (j = 0; j < fs->fs_frag; j++) {
14917937Smckusick 				if (getbmap(d + j))
15017937Smckusick 					continue;
15134141Smckusick 				setbit(cg_blksfree(newcg), i + j);
15217937Smckusick 				frags++;
15317937Smckusick 			}
15434223Smckusick 			if (frags == fs->fs_frag) {
15517937Smckusick 				newcg->cg_cs.cs_nbfree++;
15634223Smckusick 				j = cbtocylno(fs, i);
15734141Smckusick 				cg_blktot(newcg)[j]++;
15834223Smckusick 				cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++;
15917937Smckusick 			} else if (frags > 0) {
16017937Smckusick 				newcg->cg_cs.cs_nffree += frags;
16134223Smckusick 				blk = blkmap(fs, cg_blksfree(newcg), i);
16234223Smckusick 				fragacct(fs, blk, newcg->cg_frsum, 1);
16317937Smckusick 			}
16416267Smckusick 		}
16517937Smckusick 		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
16617937Smckusick 		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
16717937Smckusick 		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
16817937Smckusick 		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
16934223Smckusick 		cs = &fs->fs_cs(fs, c);
17034141Smckusick 		if (bcmp((char *)&newcg->cg_cs, (char *)cs, sizeof *cs) != 0 &&
171*37099Smckusick 		    dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
17234141Smckusick 			bcopy((char *)&newcg->cg_cs, (char *)cs, sizeof *cs);
17334141Smckusick 			sbdirty();
17434141Smckusick 		}
17534141Smckusick 		if (cvtflag) {
17634223Smckusick 			bcopy((char *)newcg, (char *)cg, fs->fs_cgsize);
17734141Smckusick 			cgdirty();
17834141Smckusick 			continue;
17934141Smckusick 		}
18034141Smckusick 		if (bcmp(cg_inosused(newcg),
18134223Smckusick 			 cg_inosused(cg), mapsize) != 0 &&
182*37099Smckusick 		    dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) {
18334223Smckusick 			bcopy(cg_inosused(newcg), cg_inosused(cg), mapsize);
18417937Smckusick 			cgdirty();
18516267Smckusick 		}
18634223Smckusick 		if ((bcmp((char *)newcg, (char *)cg, basesize) != 0 ||
18734141Smckusick 		     bcmp((char *)&cg_blktot(newcg)[0],
18834223Smckusick 			  (char *)&cg_blktot(cg)[0], sumsize) != 0) &&
189*37099Smckusick 		    dofix(&idesc[2], "SUMMARY INFORMATION BAD")) {
19034223Smckusick 			bcopy((char *)newcg, (char *)cg, basesize);
19134141Smckusick 			bcopy((char *)&cg_blktot(newcg)[0],
19234223Smckusick 			      (char *)&cg_blktot(cg)[0], sumsize);
19317937Smckusick 			cgdirty();
19416267Smckusick 		}
19516267Smckusick 	}
19634223Smckusick 	if (fs->fs_postblformat == FS_42POSTBLFMT)
19734223Smckusick 		fs->fs_nrpos = savednrpos;
19834223Smckusick 	if (bcmp((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs) != 0
199*37099Smckusick 	    && dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
20034223Smckusick 		bcopy((char *)&cstotal, (char *)&fs->fs_cstotal, sizeof *cs);
20134223Smckusick 		fs->fs_ronly = 0;
20234223Smckusick 		fs->fs_fmod = 0;
20317937Smckusick 		sbdirty();
20416267Smckusick 	}
20516267Smckusick }
206