pass4.c revision 23675
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
3523675Speterstatic const char sccsid[] = "@(#)pass4.c	8.4 (Berkeley) 4/28/95";
361558Srgrimes#endif /* not lint */
371558Srgrimes
381558Srgrimes#include <sys/param.h>
391558Srgrimes#include <sys/time.h>
4023675Speter
411558Srgrimes#include <ufs/ufs/dinode.h>
421558Srgrimes#include <ufs/ffs/fs.h>
4323675Speter
4423675Speter#include <err.h>
451558Srgrimes#include <string.h>
4623675Speter
471558Srgrimes#include "fsck.h"
481558Srgrimes
497585Sbdevoid
501558Srgrimespass4()
511558Srgrimes{
521558Srgrimes	register ino_t inumber;
531558Srgrimes	register struct zlncnt *zlnp;
541558Srgrimes	struct dinode *dp;
551558Srgrimes	struct inodesc idesc;
561558Srgrimes	int n;
571558Srgrimes
5823675Speter	memset(&idesc, 0, sizeof(struct inodesc));
591558Srgrimes	idesc.id_type = ADDR;
601558Srgrimes	idesc.id_func = pass4check;
611558Srgrimes	for (inumber = ROOTINO; inumber <= lastino; inumber++) {
621558Srgrimes		idesc.id_number = inumber;
631558Srgrimes		switch (statemap[inumber]) {
641558Srgrimes
651558Srgrimes		case FSTATE:
661558Srgrimes		case DFOUND:
671558Srgrimes			n = lncntp[inumber];
681558Srgrimes			if (n)
691558Srgrimes				adjust(&idesc, (short)n);
701558Srgrimes			else {
711558Srgrimes				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
721558Srgrimes					if (zlnp->zlncnt == inumber) {
731558Srgrimes						zlnp->zlncnt = zlnhead->zlncnt;
741558Srgrimes						zlnp = zlnhead;
751558Srgrimes						zlnhead = zlnhead->next;
761558Srgrimes						free((char *)zlnp);
771558Srgrimes						clri(&idesc, "UNREF", 1);
781558Srgrimes						break;
791558Srgrimes					}
801558Srgrimes			}
811558Srgrimes			break;
821558Srgrimes
831558Srgrimes		case DSTATE:
841558Srgrimes			clri(&idesc, "UNREF", 1);
851558Srgrimes			break;
861558Srgrimes
871558Srgrimes		case DCLEAR:
881558Srgrimes			dp = ginode(inumber);
891558Srgrimes			if (dp->di_size == 0) {
901558Srgrimes				clri(&idesc, "ZERO LENGTH", 1);
911558Srgrimes				break;
921558Srgrimes			}
931558Srgrimes			/* fall through */
941558Srgrimes		case FCLEAR:
951558Srgrimes			clri(&idesc, "BAD/DUP", 1);
961558Srgrimes			break;
971558Srgrimes
981558Srgrimes		case USTATE:
991558Srgrimes			break;
1001558Srgrimes
1011558Srgrimes		default:
10223675Speter			errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
1031558Srgrimes			    statemap[inumber], inumber);
1041558Srgrimes		}
1051558Srgrimes	}
1061558Srgrimes}
1071558Srgrimes
1087585Sbdeint
1091558Srgrimespass4check(idesc)
1101558Srgrimes	register struct inodesc *idesc;
1111558Srgrimes{
1121558Srgrimes	register struct dups *dlp;
1131558Srgrimes	int nfrags, res = KEEPON;
11423675Speter	ufs_daddr_t blkno = idesc->id_blkno;
1151558Srgrimes
1161558Srgrimes	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
1171558Srgrimes		if (chkrange(blkno, 1)) {
1181558Srgrimes			res = SKIP;
1191558Srgrimes		} else if (testbmap(blkno)) {
1201558Srgrimes			for (dlp = duplist; dlp; dlp = dlp->next) {
1211558Srgrimes				if (dlp->dup != blkno)
1221558Srgrimes					continue;
1231558Srgrimes				dlp->dup = duplist->dup;
1241558Srgrimes				dlp = duplist;
1251558Srgrimes				duplist = duplist->next;
1261558Srgrimes				free((char *)dlp);
1271558Srgrimes				break;
1281558Srgrimes			}
1291558Srgrimes			if (dlp == 0) {
1301558Srgrimes				clrbmap(blkno);
1311558Srgrimes				n_blks--;
1321558Srgrimes			}
1331558Srgrimes		}
1341558Srgrimes	}
1351558Srgrimes	return (res);
1361558Srgrimes}
137