pass4.c revision 41477
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[] =
3941477Sjulian	"$Id: pass4.c,v 1.4 1998/06/15 07:07:19 charnier Exp $";
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
531558Srgrimespass4()
541558Srgrimes{
551558Srgrimes	register ino_t inumber;
561558Srgrimes	register 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++) {
6541474Sjulian		inumber = cg * sblock.fs_ipg;
6641474Sjulian		for (i = 0; i < inostathead[cg].il_numalloced; i++, inumber++) {
6741474Sjulian			if (inumber < ROOTINO)
6841474Sjulian				continue;
6941474Sjulian			idesc.id_number = inumber;
7041474Sjulian			switch (inoinfo(inumber)->ino_state) {
711558Srgrimes
7241474Sjulian			case FSTATE:
7341474Sjulian			case DFOUND:
7441474Sjulian				n = inoinfo(inumber)->ino_linkcnt;
7541474Sjulian				if (n) {
7641474Sjulian					adjust(&idesc, (short)n);
7741474Sjulian					break;
7841474Sjulian				}
7941474Sjulian				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) {
801558Srgrimes					if (zlnp->zlncnt == inumber) {
811558Srgrimes						zlnp->zlncnt = zlnhead->zlncnt;
821558Srgrimes						zlnp = zlnhead;
831558Srgrimes						zlnhead = zlnhead->next;
841558Srgrimes						free((char *)zlnp);
851558Srgrimes						clri(&idesc, "UNREF", 1);
861558Srgrimes						break;
871558Srgrimes					}
8841474Sjulian				}
8941474Sjulian				break;
901558Srgrimes
9141474Sjulian			case DSTATE:
9241474Sjulian				clri(&idesc, "UNREF", 1);
9341474Sjulian				break;
941558Srgrimes
9541474Sjulian			case DCLEAR:
9641474Sjulian				dp = ginode(inumber);
9741474Sjulian				if (dp->di_size == 0) {
9841474Sjulian					clri(&idesc, "ZERO LENGTH", 1);
9941474Sjulian					break;
10041474Sjulian				}
10141474Sjulian				/* fall through */
10241474Sjulian			case FCLEAR:
10341474Sjulian				clri(&idesc, "BAD/DUP", 1);
1041558Srgrimes				break;
1051558Srgrimes
10641474Sjulian			case USTATE:
10741474Sjulian				break;
1081558Srgrimes
10941474Sjulian			default:
11041474Sjulian				errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
11141474Sjulian				    inoinfo(inumber)->ino_state, inumber);
11241474Sjulian			}
1131558Srgrimes		}
1141558Srgrimes	}
1151558Srgrimes}
1161558Srgrimes
1177585Sbdeint
1181558Srgrimespass4check(idesc)
1191558Srgrimes	register struct inodesc *idesc;
1201558Srgrimes{
1211558Srgrimes	register struct dups *dlp;
1221558Srgrimes	int nfrags, res = KEEPON;
12323675Speter	ufs_daddr_t blkno = idesc->id_blkno;
1241558Srgrimes
1251558Srgrimes	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
1261558Srgrimes		if (chkrange(blkno, 1)) {
1271558Srgrimes			res = SKIP;
1281558Srgrimes		} else if (testbmap(blkno)) {
1291558Srgrimes			for (dlp = duplist; dlp; dlp = dlp->next) {
1301558Srgrimes				if (dlp->dup != blkno)
1311558Srgrimes					continue;
1321558Srgrimes				dlp->dup = duplist->dup;
1331558Srgrimes				dlp = duplist;
1341558Srgrimes				duplist = duplist->next;
1351558Srgrimes				free((char *)dlp);
1361558Srgrimes				break;
1371558Srgrimes			}
1381558Srgrimes			if (dlp == 0) {
1391558Srgrimes				clrbmap(blkno);
1401558Srgrimes				n_blks--;
1411558Srgrimes			}
1421558Srgrimes		}
1431558Srgrimes	}
1441558Srgrimes	return (res);
1451558Srgrimes}
146