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 * 4. Neither the name of the University nor the names of its contributors
141558Srgrimes *    may be used to endorse or promote products derived from this software
151558Srgrimes *    without specific prior written permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes */
291558Srgrimes
30114589Sobrien#if 0
311558Srgrimes#ifndef lint
3223675Speterstatic const char sccsid[] = "@(#)pass4.c	8.4 (Berkeley) 4/28/95";
33114589Sobrien#endif /* not lint */
3441477Sjulian#endif
35114589Sobrien#include <sys/cdefs.h>
36114589Sobrien__FBSDID("$FreeBSD$");
371558Srgrimes
381558Srgrimes#include <sys/param.h>
3923675Speter
401558Srgrimes#include <ufs/ufs/dinode.h>
4141474Sjulian#include <ufs/ffs/fs.h>
4223675Speter
4323675Speter#include <err.h>
441558Srgrimes#include <string.h>
4523675Speter
461558Srgrimes#include "fsck.h"
471558Srgrimes
487585Sbdevoid
4992839Simppass4(void)
501558Srgrimes{
5192806Sobrien	ino_t inumber;
5298542Smckusick	union dinode *dp;
531558Srgrimes	struct inodesc idesc;
5441474Sjulian	int i, n, cg;
551558Srgrimes
5623675Speter	memset(&idesc, 0, sizeof(struct inodesc));
571558Srgrimes	idesc.id_type = ADDR;
581558Srgrimes	idesc.id_func = pass4check;
5941474Sjulian	for (cg = 0; cg < sblock.fs_ncg; cg++) {
6070050Siedowse		if (got_siginfo) {
6170050Siedowse			printf("%s: phase 4: cyl group %d of %d (%d%%)\n",
6270050Siedowse			    cdevname, cg, sblock.fs_ncg,
6370050Siedowse			    cg * 100 / sblock.fs_ncg);
6470050Siedowse			got_siginfo = 0;
6570050Siedowse		}
66126345Sscottl		if (got_sigalarm) {
67126345Sscottl			setproctitle("%s p4 %d%%", cdevname,
68126345Sscottl			    cg * 100 / sblock.fs_ncg);
69126345Sscottl			got_sigalarm = 0;
70126345Sscottl		}
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
78136281Struckman			case FZLINK:
79136281Struckman			case DZLINK:
80136281Struckman				if (inoinfo(inumber)->ino_linkcnt == 0) {
81136281Struckman					clri(&idesc, "UNREF", 1);
82136281Struckman					break;
83136281Struckman				}
84136281Struckman				/* fall through */
85136281Struckman
8641474Sjulian			case FSTATE:
8741474Sjulian			case DFOUND:
8841474Sjulian				n = inoinfo(inumber)->ino_linkcnt;
8941474Sjulian				if (n) {
9041474Sjulian					adjust(&idesc, (short)n);
9141474Sjulian					break;
9241474Sjulian				}
9341474Sjulian				break;
941558Srgrimes
9541474Sjulian			case DSTATE:
9641474Sjulian				clri(&idesc, "UNREF", 1);
9741474Sjulian				break;
981558Srgrimes
9941474Sjulian			case DCLEAR:
100208330Smckusick				/* if on snapshot, already cleared */
101208330Smckusick				if (cursnapshot != 0)
102208330Smckusick					break;
10341474Sjulian				dp = ginode(inumber);
10498542Smckusick				if (DIP(dp, di_size) == 0) {
10541474Sjulian					clri(&idesc, "ZERO LENGTH", 1);
10641474Sjulian					break;
10741474Sjulian				}
10841474Sjulian				/* fall through */
10941474Sjulian			case FCLEAR:
11041474Sjulian				clri(&idesc, "BAD/DUP", 1);
1111558Srgrimes				break;
1121558Srgrimes
11341474Sjulian			case USTATE:
11441474Sjulian				break;
1151558Srgrimes
11641474Sjulian			default:
11741474Sjulian				errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
11841474Sjulian				    inoinfo(inumber)->ino_state, inumber);
11941474Sjulian			}
1201558Srgrimes		}
1211558Srgrimes	}
1221558Srgrimes}
1231558Srgrimes
1247585Sbdeint
12592839Simppass4check(struct inodesc *idesc)
1261558Srgrimes{
12792806Sobrien	struct dups *dlp;
1281558Srgrimes	int nfrags, res = KEEPON;
12998542Smckusick	ufs2_daddr_t blkno = idesc->id_blkno;
1301558Srgrimes
1311558Srgrimes	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
1321558Srgrimes		if (chkrange(blkno, 1)) {
1331558Srgrimes			res = SKIP;
1341558Srgrimes		} else if (testbmap(blkno)) {
1351558Srgrimes			for (dlp = duplist; dlp; dlp = dlp->next) {
1361558Srgrimes				if (dlp->dup != blkno)
1371558Srgrimes					continue;
1381558Srgrimes				dlp->dup = duplist->dup;
1391558Srgrimes				dlp = duplist;
1401558Srgrimes				duplist = duplist->next;
1411558Srgrimes				free((char *)dlp);
1421558Srgrimes				break;
1431558Srgrimes			}
1441558Srgrimes			if (dlp == 0) {
1451558Srgrimes				clrbmap(blkno);
1461558Srgrimes				n_blks--;
1471558Srgrimes			}
1481558Srgrimes		}
1491558Srgrimes	}
1501558Srgrimes	return (res);
1511558Srgrimes}
152