pass4.c revision 41474
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>
3941474Sjulian#include <sys/time.h>
4023675Speter
411558Srgrimes#include <ufs/ufs/dinode.h>
4241474Sjulian#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;
5641474Sjulian	int i, n, cg;
571558Srgrimes
5823675Speter	memset(&idesc, 0, sizeof(struct inodesc));
591558Srgrimes	idesc.id_type = ADDR;
601558Srgrimes	idesc.id_func = pass4check;
6141474Sjulian	for (cg = 0; cg < sblock.fs_ncg; cg++) {
6241474Sjulian		inumber = cg * sblock.fs_ipg;
6341474Sjulian		for (i = 0; i < inostathead[cg].il_numalloced; i++, inumber++) {
6441474Sjulian			if (inumber < ROOTINO)
6541474Sjulian				continue;
6641474Sjulian			idesc.id_number = inumber;
6741474Sjulian			switch (inoinfo(inumber)->ino_state) {
681558Srgrimes
6941474Sjulian			case FSTATE:
7041474Sjulian			case DFOUND:
7141474Sjulian				n = inoinfo(inumber)->ino_linkcnt;
7241474Sjulian				if (n) {
7341474Sjulian					adjust(&idesc, (short)n);
7441474Sjulian					break;
7541474Sjulian				}
7641474Sjulian				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) {
771558Srgrimes					if (zlnp->zlncnt == inumber) {
781558Srgrimes						zlnp->zlncnt = zlnhead->zlncnt;
791558Srgrimes						zlnp = zlnhead;
801558Srgrimes						zlnhead = zlnhead->next;
811558Srgrimes						free((char *)zlnp);
821558Srgrimes						clri(&idesc, "UNREF", 1);
831558Srgrimes						break;
841558Srgrimes					}
8541474Sjulian				}
8641474Sjulian				break;
871558Srgrimes
8841474Sjulian			case DSTATE:
8941474Sjulian				clri(&idesc, "UNREF", 1);
9041474Sjulian				break;
911558Srgrimes
9241474Sjulian			case DCLEAR:
9341474Sjulian				dp = ginode(inumber);
9441474Sjulian				if (dp->di_size == 0) {
9541474Sjulian					clri(&idesc, "ZERO LENGTH", 1);
9641474Sjulian					break;
9741474Sjulian				}
9841474Sjulian				/* fall through */
9941474Sjulian			case FCLEAR:
10041474Sjulian				clri(&idesc, "BAD/DUP", 1);
1011558Srgrimes				break;
1021558Srgrimes
10341474Sjulian			case USTATE:
10441474Sjulian				break;
1051558Srgrimes
10641474Sjulian			default:
10741474Sjulian				errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
10841474Sjulian				    inoinfo(inumber)->ino_state, inumber);
10941474Sjulian			}
1101558Srgrimes		}
1111558Srgrimes	}
1121558Srgrimes}
1131558Srgrimes
1147585Sbdeint
1151558Srgrimespass4check(idesc)
1161558Srgrimes	register struct inodesc *idesc;
1171558Srgrimes{
1181558Srgrimes	register struct dups *dlp;
1191558Srgrimes	int nfrags, res = KEEPON;
12023675Speter	ufs_daddr_t blkno = idesc->id_blkno;
1211558Srgrimes
1221558Srgrimes	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
1231558Srgrimes		if (chkrange(blkno, 1)) {
1241558Srgrimes			res = SKIP;
1251558Srgrimes		} else if (testbmap(blkno)) {
1261558Srgrimes			for (dlp = duplist; dlp; dlp = dlp->next) {
1271558Srgrimes				if (dlp->dup != blkno)
1281558Srgrimes					continue;
1291558Srgrimes				dlp->dup = duplist->dup;
1301558Srgrimes				dlp = duplist;
1311558Srgrimes				duplist = duplist->next;
1321558Srgrimes				free((char *)dlp);
1331558Srgrimes				break;
1341558Srgrimes			}
1351558Srgrimes			if (dlp == 0) {
1361558Srgrimes				clrbmap(blkno);
1371558Srgrimes				n_blks--;
1381558Srgrimes			}
1391558Srgrimes		}
1401558Srgrimes	}
1411558Srgrimes	return (res);
1421558Srgrimes}
143