pass4.c revision 136281
178344Sobrien/*
278344Sobrien * Copyright (c) 1980, 1986, 1993
398184Sgordon *	The Regents of the University of California.  All rights reserved.
478344Sobrien *
578344Sobrien * Redistribution and use in source and binary forms, with or without
678344Sobrien * modification, are permitted provided that the following conditions
7240336Sobrien * are met:
8180564Sdougb * 1. Redistributions of source code must retain the above copyright
978344Sobrien *    notice, this list of conditions and the following disclaimer.
1078344Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1178344Sobrien *    notice, this list of conditions and the following disclaimer in the
1278344Sobrien *    documentation and/or other materials provided with the distribution.
13230099Sdougb * 4. Neither the name of the University nor the names of its contributors
14151586Syar *    may be used to endorse or promote products derived from this software
1598184Sgordon *    without specific prior written permission.
1698184Sgordon *
17255766Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18255766Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19240109Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2078344Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21240109Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2278344Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23255766Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24255766Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25255766Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26255766Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27262566Sdes * SUCH DAMAGE.
28133110Smarkm */
29255766Sdes
30133110Smarkm#if 0
31255766Sdes#ifndef lint
32255766Sdesstatic const char sccsid[] = "@(#)pass4.c	8.4 (Berkeley) 4/28/95";
33255766Sdes#endif /* not lint */
34255766Sdes#endif
35255766Sdes#include <sys/cdefs.h>
36255766Sdes__FBSDID("$FreeBSD: head/sbin/fsck_ffs/pass4.c 136281 2004-10-08 20:44:47Z truckman $");
37133110Smarkm
38133110Smarkm#include <sys/param.h>
39255766Sdes
40255766Sdes#include <ufs/ufs/dinode.h>
41255766Sdes#include <ufs/ffs/fs.h>
42255766Sdes
43262566Sdes#include <err.h>
44255766Sdes#include <string.h>
45255766Sdes
46255766Sdes#include "fsck.h"
47255766Sdes
48255766Sdesvoid
49255766Sdespass4(void)
5098184Sgordon{
51255766Sdes	ino_t inumber;
52161530Sflz	union dinode *dp;
5398184Sgordon	struct inodesc idesc;
5478344Sobrien	int i, n, cg;
5578344Sobrien
56255766Sdes	memset(&idesc, 0, sizeof(struct inodesc));
57256126Sdelphij	idesc.id_type = ADDR;
5878344Sobrien	idesc.id_func = pass4check;
59255766Sdes	for (cg = 0; cg < sblock.fs_ncg; cg++) {
60255766Sdes		if (got_siginfo) {
61255766Sdes			printf("%s: phase 4: cyl group %d of %d (%d%%)\n",
6278344Sobrien			    cdevname, cg, sblock.fs_ncg,
63255766Sdes			    cg * 100 / sblock.fs_ncg);
6498184Sgordon			got_siginfo = 0;
65255766Sdes		}
66255766Sdes		if (got_sigalarm) {
67255766Sdes			setproctitle("%s p4 %d%%", cdevname,
68255766Sdes			    cg * 100 / sblock.fs_ncg);
69255766Sdes			got_sigalarm = 0;
70255766Sdes		}
71262566Sdes		inumber = cg * sblock.fs_ipg;
7278344Sobrien		for (i = 0; i < inostathead[cg].il_numalloced; i++, inumber++) {
7378344Sobrien			if (inumber < ROOTINO)
74240109Sdes				continue;
75240109Sdes			idesc.id_number = inumber;
76240109Sdes			switch (inoinfo(inumber)->ino_state) {
77240109Sdes
78240109Sdes			case FZLINK:
79240109Sdes			case DZLINK:
8078344Sobrien				if (inoinfo(inumber)->ino_linkcnt == 0) {
8178344Sobrien					clri(&idesc, "UNREF", 1);
82255766Sdes					break;
83255766Sdes				}
8478344Sobrien				/* fall through */
8578344Sobrien
86161530Sflz			case FSTATE:
8778344Sobrien			case DFOUND:
88				n = inoinfo(inumber)->ino_linkcnt;
89				if (n) {
90					adjust(&idesc, (short)n);
91					break;
92				}
93				break;
94
95			case DSTATE:
96				clri(&idesc, "UNREF", 1);
97				break;
98
99			case DCLEAR:
100				dp = ginode(inumber);
101				if (DIP(dp, di_size) == 0) {
102					clri(&idesc, "ZERO LENGTH", 1);
103					break;
104				}
105				/* fall through */
106			case FCLEAR:
107				clri(&idesc, "BAD/DUP", 1);
108				break;
109
110			case USTATE:
111				break;
112
113			default:
114				errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
115				    inoinfo(inumber)->ino_state, inumber);
116			}
117		}
118	}
119}
120
121int
122pass4check(struct inodesc *idesc)
123{
124	struct dups *dlp;
125	int nfrags, res = KEEPON;
126	ufs2_daddr_t blkno = idesc->id_blkno;
127
128	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
129		if (chkrange(blkno, 1)) {
130			res = SKIP;
131		} else if (testbmap(blkno)) {
132			for (dlp = duplist; dlp; dlp = dlp->next) {
133				if (dlp->dup != blkno)
134					continue;
135				dlp->dup = duplist->dup;
136				dlp = duplist;
137				duplist = duplist->next;
138				free((char *)dlp);
139				break;
140			}
141			if (dlp == 0) {
142				clrbmap(blkno);
143				n_blks--;
144			}
145		}
146	}
147	return (res);
148}
149