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>
44241012Smdf#include <stdint.h>
451558Srgrimes#include <string.h>
4623675Speter
471558Srgrimes#include "fsck.h"
481558Srgrimes
497585Sbdevoid
5092839Simppass4(void)
511558Srgrimes{
5292806Sobrien	ino_t inumber;
5398542Smckusick	union dinode *dp;
541558Srgrimes	struct inodesc idesc;
5541474Sjulian	int i, n, cg;
561558Srgrimes
5723675Speter	memset(&idesc, 0, sizeof(struct inodesc));
581558Srgrimes	idesc.id_type = ADDR;
591558Srgrimes	idesc.id_func = pass4check;
6041474Sjulian	for (cg = 0; cg < sblock.fs_ncg; cg++) {
6170050Siedowse		if (got_siginfo) {
6270050Siedowse			printf("%s: phase 4: cyl group %d of %d (%d%%)\n",
6370050Siedowse			    cdevname, cg, sblock.fs_ncg,
6470050Siedowse			    cg * 100 / sblock.fs_ncg);
6570050Siedowse			got_siginfo = 0;
6670050Siedowse		}
67126345Sscottl		if (got_sigalarm) {
68126345Sscottl			setproctitle("%s p4 %d%%", cdevname,
69126345Sscottl			    cg * 100 / sblock.fs_ncg);
70126345Sscottl			got_sigalarm = 0;
71126345Sscottl		}
7241474Sjulian		inumber = cg * sblock.fs_ipg;
7341474Sjulian		for (i = 0; i < inostathead[cg].il_numalloced; i++, inumber++) {
7441474Sjulian			if (inumber < ROOTINO)
7541474Sjulian				continue;
7641474Sjulian			idesc.id_number = inumber;
7741474Sjulian			switch (inoinfo(inumber)->ino_state) {
781558Srgrimes
79136281Struckman			case FZLINK:
80136281Struckman			case DZLINK:
81136281Struckman				if (inoinfo(inumber)->ino_linkcnt == 0) {
82136281Struckman					clri(&idesc, "UNREF", 1);
83136281Struckman					break;
84136281Struckman				}
85136281Struckman				/* fall through */
86136281Struckman
8741474Sjulian			case FSTATE:
8841474Sjulian			case DFOUND:
8941474Sjulian				n = inoinfo(inumber)->ino_linkcnt;
9041474Sjulian				if (n) {
9141474Sjulian					adjust(&idesc, (short)n);
9241474Sjulian					break;
9341474Sjulian				}
9441474Sjulian				break;
951558Srgrimes
9641474Sjulian			case DSTATE:
9741474Sjulian				clri(&idesc, "UNREF", 1);
9841474Sjulian				break;
991558Srgrimes
10041474Sjulian			case DCLEAR:
101208330Smckusick				/* if on snapshot, already cleared */
102208330Smckusick				if (cursnapshot != 0)
103208330Smckusick					break;
10441474Sjulian				dp = ginode(inumber);
10598542Smckusick				if (DIP(dp, di_size) == 0) {
10641474Sjulian					clri(&idesc, "ZERO LENGTH", 1);
10741474Sjulian					break;
10841474Sjulian				}
10941474Sjulian				/* fall through */
11041474Sjulian			case FCLEAR:
11141474Sjulian				clri(&idesc, "BAD/DUP", 1);
1121558Srgrimes				break;
1131558Srgrimes
11441474Sjulian			case USTATE:
11541474Sjulian				break;
1161558Srgrimes
11741474Sjulian			default:
118241012Smdf				errx(EEXIT, "BAD STATE %d FOR INODE I=%ju",
119241012Smdf				    inoinfo(inumber)->ino_state,
120241012Smdf				    (uintmax_t)inumber);
12141474Sjulian			}
1221558Srgrimes		}
1231558Srgrimes	}
1241558Srgrimes}
1251558Srgrimes
1267585Sbdeint
12792839Simppass4check(struct inodesc *idesc)
1281558Srgrimes{
12992806Sobrien	struct dups *dlp;
1301558Srgrimes	int nfrags, res = KEEPON;
13198542Smckusick	ufs2_daddr_t blkno = idesc->id_blkno;
1321558Srgrimes
1331558Srgrimes	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
1341558Srgrimes		if (chkrange(blkno, 1)) {
1351558Srgrimes			res = SKIP;
1361558Srgrimes		} else if (testbmap(blkno)) {
1371558Srgrimes			for (dlp = duplist; dlp; dlp = dlp->next) {
1381558Srgrimes				if (dlp->dup != blkno)
1391558Srgrimes					continue;
1401558Srgrimes				dlp->dup = duplist->dup;
1411558Srgrimes				dlp = duplist;
1421558Srgrimes				duplist = duplist->next;
1431558Srgrimes				free((char *)dlp);
1441558Srgrimes				break;
1451558Srgrimes			}
1461558Srgrimes			if (dlp == 0) {
1471558Srgrimes				clrbmap(blkno);
1481558Srgrimes				n_blks--;
1491558Srgrimes			}
1501558Srgrimes		}
1511558Srgrimes	}
1521558Srgrimes	return (res);
1531558Srgrimes}
154