pass4.c revision 92839
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1980, 1986, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifndef lint
3541477Sjulian#if 0
3623675Speterstatic const char sccsid[] = "@(#)pass4.c	8.4 (Berkeley) 4/28/95";
3741477Sjulian#endif
3841477Sjulianstatic const char rcsid[] =
3950476Speter  "$FreeBSD: head/sbin/fsck_ffs/pass4.c 92839 2002-03-20 22:57:10Z imp $";
401558Srgrimes#endif /* not lint */
411558Srgrimes
421558Srgrimes#include <sys/param.h>
4323675Speter
441558Srgrimes#include <ufs/ufs/dinode.h>
4541474Sjulian#include <ufs/ffs/fs.h>
4623675Speter
4723675Speter#include <err.h>
481558Srgrimes#include <string.h>
4923675Speter
501558Srgrimes#include "fsck.h"
511558Srgrimes
527585Sbdevoid
5392839Simppass4(void)
541558Srgrimes{
5592806Sobrien	ino_t inumber;
5692806Sobrien	struct zlncnt *zlnp;
571558Srgrimes	struct dinode *dp;
581558Srgrimes	struct inodesc idesc;
5941474Sjulian	int i, n, cg;
601558Srgrimes
6123675Speter	memset(&idesc, 0, sizeof(struct inodesc));
621558Srgrimes	idesc.id_type = ADDR;
631558Srgrimes	idesc.id_func = pass4check;
6441474Sjulian	for (cg = 0; cg < sblock.fs_ncg; cg++) {
6570050Siedowse		if (got_siginfo) {
6670050Siedowse			printf("%s: phase 4: cyl group %d of %d (%d%%)\n",
6770050Siedowse			    cdevname, cg, sblock.fs_ncg,
6870050Siedowse			    cg * 100 / sblock.fs_ncg);
6970050Siedowse			got_siginfo = 0;
7070050Siedowse		}
7141474Sjulian		inumber = cg * sblock.fs_ipg;
7241474Sjulian		for (i = 0; i < inostathead[cg].il_numalloced; i++, inumber++) {
7341474Sjulian			if (inumber < ROOTINO)
7441474Sjulian				continue;
7541474Sjulian			idesc.id_number = inumber;
7641474Sjulian			switch (inoinfo(inumber)->ino_state) {
771558Srgrimes
7841474Sjulian			case FSTATE:
7941474Sjulian			case DFOUND:
8041474Sjulian				n = inoinfo(inumber)->ino_linkcnt;
8141474Sjulian				if (n) {
8241474Sjulian					adjust(&idesc, (short)n);
8341474Sjulian					break;
8441474Sjulian				}
8541474Sjulian				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) {
861558Srgrimes					if (zlnp->zlncnt == inumber) {
871558Srgrimes						zlnp->zlncnt = zlnhead->zlncnt;
881558Srgrimes						zlnp = zlnhead;
891558Srgrimes						zlnhead = zlnhead->next;
901558Srgrimes						free((char *)zlnp);
911558Srgrimes						clri(&idesc, "UNREF", 1);
921558Srgrimes						break;
931558Srgrimes					}
9441474Sjulian				}
9541474Sjulian				break;
961558Srgrimes
9741474Sjulian			case DSTATE:
9841474Sjulian				clri(&idesc, "UNREF", 1);
9941474Sjulian				break;
1001558Srgrimes
10141474Sjulian			case DCLEAR:
10241474Sjulian				dp = ginode(inumber);
10341474Sjulian				if (dp->di_size == 0) {
10441474Sjulian					clri(&idesc, "ZERO LENGTH", 1);
10541474Sjulian					break;
10641474Sjulian				}
10741474Sjulian				/* fall through */
10841474Sjulian			case FCLEAR:
10941474Sjulian				clri(&idesc, "BAD/DUP", 1);
1101558Srgrimes				break;
1111558Srgrimes
11241474Sjulian			case USTATE:
11341474Sjulian				break;
1141558Srgrimes
11541474Sjulian			default:
11641474Sjulian				errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
11741474Sjulian				    inoinfo(inumber)->ino_state, inumber);
11841474Sjulian			}
1191558Srgrimes		}
1201558Srgrimes	}
1211558Srgrimes}
1221558Srgrimes
1237585Sbdeint
12492839Simppass4check(struct inodesc *idesc)
1251558Srgrimes{
12692806Sobrien	struct dups *dlp;
1271558Srgrimes	int nfrags, res = KEEPON;
12823675Speter	ufs_daddr_t blkno = idesc->id_blkno;
1291558Srgrimes
1301558Srgrimes	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
1311558Srgrimes		if (chkrange(blkno, 1)) {
1321558Srgrimes			res = SKIP;
1331558Srgrimes		} else if (testbmap(blkno)) {
1341558Srgrimes			for (dlp = duplist; dlp; dlp = dlp->next) {
1351558Srgrimes				if (dlp->dup != blkno)
1361558Srgrimes					continue;
1371558Srgrimes				dlp->dup = duplist->dup;
1381558Srgrimes				dlp = duplist;
1391558Srgrimes				duplist = duplist->next;
1401558Srgrimes				free((char *)dlp);
1411558Srgrimes				break;
1421558Srgrimes			}
1431558Srgrimes			if (dlp == 0) {
1441558Srgrimes				clrbmap(blkno);
1451558Srgrimes				n_blks--;
1461558Srgrimes			}
1471558Srgrimes		}
1481558Srgrimes	}
1491558Srgrimes	return (res);
1501558Srgrimes}
151