xref: /csrg-svn/sbin/fsck/pass5.c (revision 38337)
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*38337Smckusick static char sccsid[] = "@(#)pass5.c	5.7 (Berkeley) 06/26/89";
922053Sdist #endif not lint
1016267Smckusick 
1116267Smckusick #include <sys/param.h>
12*38337Smckusick #include <sys/time.h>
13*38337Smckusick #include <sys/vnode.h>
14*38337Smckusick #include <ufs/inode.h>
15*38337Smckusick #include <ufs/fs.h>
1616267Smckusick #include "fsck.h"
1716267Smckusick 
1816267Smckusick pass5()
1916267Smckusick {
2034141Smckusick 	int c, blk, frags, basesize, sumsize, mapsize, savednrpos;
2134223Smckusick 	register struct fs *fs = &sblock;
2234223Smckusick 	register struct cg *cg = &cgrp;
2334141Smckusick 	daddr_t dbase, dmax;
2434141Smckusick 	register daddr_t d;
2517937Smckusick 	register long i, j;
2617937Smckusick 	struct csum *cs;
2717937Smckusick 	time_t now;
2817937Smckusick 	struct csum cstotal;
2937099Smckusick 	struct inodesc idesc[3];
3017937Smckusick 	char buf[MAXBSIZE];
3117937Smckusick 	register struct cg *newcg = (struct cg *)buf;
3234141Smckusick 	struct ocg *ocg = (struct ocg *)buf;
3316267Smckusick 
3434223Smckusick 	bzero((char *)newcg, fs->fs_cgsize);
3534223Smckusick 	newcg->cg_niblk = fs->fs_ipg;
3634223Smckusick 	switch (fs->fs_postblformat) {
3734141Smckusick 
3834141Smckusick 	case FS_42POSTBLFMT:
3934141Smckusick 		basesize = (char *)(&ocg->cg_btot[0]) - (char *)(&ocg->cg_link);
4034141Smckusick 		sumsize = &ocg->cg_iused[0] - (char *)(&ocg->cg_btot[0]);
4134223Smckusick 		mapsize = &ocg->cg_free[howmany(fs->fs_fpg, NBBY)] -
4234141Smckusick 			(u_char *)&ocg->cg_iused[0];
4334141Smckusick 		ocg->cg_magic = CG_MAGIC;
4434223Smckusick 		savednrpos = fs->fs_nrpos;
4534223Smckusick 		fs->fs_nrpos = 8;
4634141Smckusick 		break;
4734141Smckusick 
4834141Smckusick 	case FS_DYNAMICPOSTBLFMT:
4934141Smckusick 		newcg->cg_btotoff =
5034141Smckusick 		 	&newcg->cg_space[0] - (u_char *)(&newcg->cg_link);
5134141Smckusick 		newcg->cg_boff =
5234223Smckusick 			newcg->cg_btotoff + fs->fs_cpg * sizeof(long);
5334141Smckusick 		newcg->cg_iusedoff = newcg->cg_boff +
5434223Smckusick 			fs->fs_cpg * fs->fs_nrpos * sizeof(short);
5534141Smckusick 		newcg->cg_freeoff =
5634223Smckusick 			newcg->cg_iusedoff + howmany(fs->fs_ipg, NBBY);
5734141Smckusick 		newcg->cg_nextfreeoff = newcg->cg_freeoff +
5834223Smckusick 			howmany(fs->fs_cpg * fs->fs_spc / NSPF(fs),
5934141Smckusick 				NBBY);
6034141Smckusick 		newcg->cg_magic = CG_MAGIC;
6134141Smckusick 		basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_link);
6234141Smckusick 		sumsize = newcg->cg_iusedoff - newcg->cg_btotoff;
6334141Smckusick 		mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
6434141Smckusick 		break;
6534141Smckusick 
6634141Smckusick 	default:
6734141Smckusick 		errexit("UNKNOWN ROTATIONAL TABLE FORMAT %d\n",
6834223Smckusick 			fs->fs_postblformat);
6934141Smckusick 	}
7037099Smckusick 	bzero((char *)&idesc[0], sizeof idesc);
7137099Smckusick 	for (i = 0; i < 3; i++)
7237099Smckusick 		idesc[i].id_type = ADDR;
7317937Smckusick 	bzero((char *)&cstotal, sizeof(struct csum));
7417937Smckusick 	(void)time(&now);
7534223Smckusick 	for (i = fs->fs_size; i < fragroundup(fs, fs->fs_size); 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++) {
15117937Smckusick 				if (getbmap(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) {
17834223Smckusick 			bcopy((char *)newcg, (char *)cg, 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