pass4.c revision 330897
1290650Shselasky/*-
2321992Shselasky * SPDX-License-Identifier: BSD-3-Clause
3290650Shselasky *
4290650Shselasky * Copyright (c) 1980, 1986, 1993
5290650Shselasky *	The Regents of the University of California.  All rights reserved.
6290650Shselasky *
7290650Shselasky * Redistribution and use in source and binary forms, with or without
8290650Shselasky * modification, are permitted provided that the following conditions
9290650Shselasky * are met:
10290650Shselasky * 1. Redistributions of source code must retain the above copyright
11290650Shselasky *    notice, this list of conditions and the following disclaimer.
12290650Shselasky * 2. Redistributions in binary form must reproduce the above copyright
13290650Shselasky *    notice, this list of conditions and the following disclaimer in the
14290650Shselasky *    documentation and/or other materials provided with the distribution.
15290650Shselasky * 4. Neither the name of the University nor the names of its contributors
16290650Shselasky *    may be used to endorse or promote products derived from this software
17290650Shselasky *    without specific prior written permission.
18290650Shselasky *
19290650Shselasky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20290650Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21290650Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22290650Shselasky * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23290650Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24290650Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25290650Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26321992Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27290650Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28290650Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29290650Shselasky * SUCH DAMAGE.
30290650Shselasky */
31290650Shselasky
32290650Shselasky#if 0
33290650Shselasky#ifndef lint
34290650Shselaskystatic const char sccsid[] = "@(#)pass4.c	8.4 (Berkeley) 4/28/95";
35290650Shselasky#endif /* not lint */
36290650Shselasky#endif
37290650Shselasky#include <sys/cdefs.h>
38290650Shselasky__FBSDID("$FreeBSD: stable/11/sbin/fsck_ffs/pass4.c 330897 2018-03-14 03:19:51Z eadler $");
39290650Shselasky
40290650Shselasky#include <sys/param.h>
41290650Shselasky
42290650Shselasky#include <ufs/ufs/dinode.h>
43290650Shselasky#include <ufs/ffs/fs.h>
44290650Shselasky
45290650Shselasky#include <err.h>
46290650Shselasky#include <stdint.h>
47290650Shselasky#include <string.h>
48290650Shselasky
49290650Shselasky#include "fsck.h"
50290650Shselasky
51306233Shselaskyvoid
52290650Shselaskypass4(void)
53306233Shselasky{
54321992Shselasky	ino_t inumber;
55321992Shselasky	union dinode *dp;
56290650Shselasky	struct inodesc idesc;
57290650Shselasky	int i, n, cg;
58290650Shselasky
59290650Shselasky	memset(&idesc, 0, sizeof(struct inodesc));
60290650Shselasky	idesc.id_type = ADDR;
61290650Shselasky	idesc.id_func = pass4check;
62290650Shselasky	for (cg = 0; cg < sblock.fs_ncg; cg++) {
63290650Shselasky		if (got_siginfo) {
64290650Shselasky			printf("%s: phase 4: cyl group %d of %d (%d%%)\n",
65306233Shselasky			    cdevname, cg, sblock.fs_ncg,
66306233Shselasky			    cg * 100 / sblock.fs_ncg);
67306233Shselasky			got_siginfo = 0;
68306233Shselasky		}
69306233Shselasky		if (got_sigalarm) {
70290650Shselasky			setproctitle("%s p4 %d%%", cdevname,
71290650Shselasky			    cg * 100 / sblock.fs_ncg);
72290650Shselasky			got_sigalarm = 0;
73290650Shselasky		}
74290650Shselasky		inumber = cg * sblock.fs_ipg;
75290650Shselasky		for (i = 0; i < inostathead[cg].il_numalloced; i++, inumber++) {
76290650Shselasky			if (inumber < ROOTINO)
77329209Shselasky				continue;
78329209Shselasky			idesc.id_number = inumber;
79329209Shselasky			switch (inoinfo(inumber)->ino_state) {
80329209Shselasky
81329209Shselasky			case FZLINK:
82290650Shselasky			case DZLINK:
83290650Shselasky				if (inoinfo(inumber)->ino_linkcnt == 0) {
84290650Shselasky					clri(&idesc, "UNREF", 1);
85290650Shselasky					break;
86290650Shselasky				}
87290650Shselasky				/* fall through */
88290650Shselasky
89290650Shselasky			case FSTATE:
90290650Shselasky			case DFOUND:
91290650Shselasky				n = inoinfo(inumber)->ino_linkcnt;
92290650Shselasky				if (n) {
93290650Shselasky					adjust(&idesc, (short)n);
94321992Shselasky					break;
95321992Shselasky				}
96290650Shselasky				break;
97290650Shselasky
98290650Shselasky			case DSTATE:
99290650Shselasky				clri(&idesc, "UNREF", 1);
100290650Shselasky				break;
101290650Shselasky
102290650Shselasky			case DCLEAR:
103290650Shselasky				/* if on snapshot, already cleared */
104290650Shselasky				if (cursnapshot != 0)
105290650Shselasky					break;
106290650Shselasky				dp = ginode(inumber);
107290650Shselasky				if (DIP(dp, di_size) == 0) {
108290650Shselasky					clri(&idesc, "ZERO LENGTH", 1);
109290650Shselasky					break;
110290650Shselasky				}
111290650Shselasky				/* fall through */
112290650Shselasky			case FCLEAR:
113290650Shselasky				clri(&idesc, "BAD/DUP", 1);
114290650Shselasky				break;
115290650Shselasky
116290650Shselasky			case USTATE:
117290650Shselasky				break;
118290650Shselasky
119290650Shselasky			default:
120290650Shselasky				errx(EEXIT, "BAD STATE %d FOR INODE I=%ju",
121290650Shselasky				    inoinfo(inumber)->ino_state,
122290650Shselasky				    (uintmax_t)inumber);
123290650Shselasky			}
124290650Shselasky		}
125290650Shselasky	}
126290650Shselasky}
127290650Shselasky
128290650Shselaskyint
129290650Shselaskypass4check(struct inodesc *idesc)
130290650Shselasky{
131290650Shselasky	struct dups *dlp;
132290650Shselasky	int nfrags, res = KEEPON;
133290650Shselasky	ufs2_daddr_t blkno = idesc->id_blkno;
134290650Shselasky
135290650Shselasky	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
136290650Shselasky		if (chkrange(blkno, 1)) {
137290650Shselasky			res = SKIP;
138290650Shselasky		} else if (testbmap(blkno)) {
139290650Shselasky			for (dlp = duplist; dlp; dlp = dlp->next) {
140290650Shselasky				if (dlp->dup != blkno)
141290650Shselasky					continue;
142290650Shselasky				dlp->dup = duplist->dup;
143290650Shselasky				dlp = duplist;
144290650Shselasky				duplist = duplist->next;
145290650Shselasky				free((char *)dlp);
146290650Shselasky				break;
147290650Shselasky			}
148290650Shselasky			if (dlp == NULL) {
149290650Shselasky				clrbmap(blkno);
150290650Shselasky				n_blks--;
151290650Shselasky			}
152290650Shselasky		}
153290650Shselasky	}
154306233Shselasky	return (res);
155306233Shselasky}
156308678Shselasky