xref: /freebsd/sbin/fsck_ffs/pass4.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
18fae3551SRodney W. Grimes /*
28fae3551SRodney W. Grimes  * Copyright (c) 1980, 1986, 1993
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
68fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
78fae3551SRodney W. Grimes  * are met:
88fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
98fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
108fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
118fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
128fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
148fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
158fae3551SRodney W. Grimes  *    without specific prior written permission.
168fae3551SRodney W. Grimes  *
178fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
188fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
218fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278fae3551SRodney W. Grimes  * SUCH DAMAGE.
288fae3551SRodney W. Grimes  */
298fae3551SRodney W. Grimes 
306b100474SJulian Elischer #if 0
31c69284caSDavid E. O'Brien #ifndef lint
32780a5c1eSPeter Wemm static const char sccsid[] = "@(#)pass4.c	8.4 (Berkeley) 4/28/95";
338fae3551SRodney W. Grimes #endif /* not lint */
34c69284caSDavid E. O'Brien #endif
35c69284caSDavid E. O'Brien #include <sys/cdefs.h>
36c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$");
378fae3551SRodney W. Grimes 
388fae3551SRodney W. Grimes #include <sys/param.h>
39780a5c1eSPeter Wemm 
408fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
41d33e92f9SJulian Elischer #include <ufs/ffs/fs.h>
428fae3551SRodney W. Grimes 
43780a5c1eSPeter Wemm #include <err.h>
44623d7cb6SMatthew D Fleming #include <stdint.h>
45780a5c1eSPeter Wemm #include <string.h>
46780a5c1eSPeter Wemm 
47780a5c1eSPeter Wemm #include "fsck.h"
488fae3551SRodney W. Grimes 
4931f4ab50SBruce Evans void
50b70cd7eeSWarner Losh pass4(void)
518fae3551SRodney W. Grimes {
523d438ad6SDavid E. O'Brien 	ino_t inumber;
531c85e6a3SKirk McKusick 	union dinode *dp;
548fae3551SRodney W. Grimes 	struct inodesc idesc;
55d33e92f9SJulian Elischer 	int i, n, cg;
568fae3551SRodney W. Grimes 
57780a5c1eSPeter Wemm 	memset(&idesc, 0, sizeof(struct inodesc));
588fae3551SRodney W. Grimes 	idesc.id_type = ADDR;
598fae3551SRodney W. Grimes 	idesc.id_func = pass4check;
60d33e92f9SJulian Elischer 	for (cg = 0; cg < sblock.fs_ncg; cg++) {
616db798caSIan Dowse 		if (got_siginfo) {
626db798caSIan Dowse 			printf("%s: phase 4: cyl group %d of %d (%d%%)\n",
636db798caSIan Dowse 			    cdevname, cg, sblock.fs_ncg,
646db798caSIan Dowse 			    cg * 100 / sblock.fs_ncg);
656db798caSIan Dowse 			got_siginfo = 0;
666db798caSIan Dowse 		}
671660ae87SScott Long 		if (got_sigalarm) {
681660ae87SScott Long 			setproctitle("%s p4 %d%%", cdevname,
691660ae87SScott Long 			    cg * 100 / sblock.fs_ncg);
701660ae87SScott Long 			got_sigalarm = 0;
711660ae87SScott Long 		}
72d33e92f9SJulian Elischer 		inumber = cg * sblock.fs_ipg;
73d33e92f9SJulian Elischer 		for (i = 0; i < inostathead[cg].il_numalloced; i++, inumber++) {
741dc349abSEd Maste 			if (inumber < UFS_ROOTINO)
75d33e92f9SJulian Elischer 				continue;
768fae3551SRodney W. Grimes 			idesc.id_number = inumber;
77d33e92f9SJulian Elischer 			switch (inoinfo(inumber)->ino_state) {
788fae3551SRodney W. Grimes 
79af6726e6SDon Lewis 			case FZLINK:
80af6726e6SDon Lewis 			case DZLINK:
81af6726e6SDon Lewis 				if (inoinfo(inumber)->ino_linkcnt == 0) {
82af6726e6SDon Lewis 					clri(&idesc, "UNREF", 1);
83af6726e6SDon Lewis 					break;
84af6726e6SDon Lewis 				}
85af6726e6SDon Lewis 				/* fall through */
86af6726e6SDon Lewis 
878fae3551SRodney W. Grimes 			case FSTATE:
888fae3551SRodney W. Grimes 			case DFOUND:
89d33e92f9SJulian Elischer 				n = inoinfo(inumber)->ino_linkcnt;
90d33e92f9SJulian Elischer 				if (n) {
918fae3551SRodney W. Grimes 					adjust(&idesc, (short)n);
92d33e92f9SJulian Elischer 					break;
93d33e92f9SJulian Elischer 				}
948fae3551SRodney W. Grimes 				break;
958fae3551SRodney W. Grimes 
968fae3551SRodney W. Grimes 			case DSTATE:
978fae3551SRodney W. Grimes 				clri(&idesc, "UNREF", 1);
988fae3551SRodney W. Grimes 				break;
998fae3551SRodney W. Grimes 
1008fae3551SRodney W. Grimes 			case DCLEAR:
10197fea87bSKirk McKusick 				/* if on snapshot, already cleared */
10297fea87bSKirk McKusick 				if (cursnapshot != 0)
10397fea87bSKirk McKusick 					break;
1048fae3551SRodney W. Grimes 				dp = ginode(inumber);
1051c85e6a3SKirk McKusick 				if (DIP(dp, di_size) == 0) {
1068fae3551SRodney W. Grimes 					clri(&idesc, "ZERO LENGTH", 1);
1078fae3551SRodney W. Grimes 					break;
1088fae3551SRodney W. Grimes 				}
1098fae3551SRodney W. Grimes 				/* fall through */
1108fae3551SRodney W. Grimes 			case FCLEAR:
1118fae3551SRodney W. Grimes 				clri(&idesc, "BAD/DUP", 1);
1128fae3551SRodney W. Grimes 				break;
1138fae3551SRodney W. Grimes 
1148fae3551SRodney W. Grimes 			case USTATE:
1158fae3551SRodney W. Grimes 				break;
1168fae3551SRodney W. Grimes 
1178fae3551SRodney W. Grimes 			default:
118623d7cb6SMatthew D Fleming 				errx(EEXIT, "BAD STATE %d FOR INODE I=%ju",
119623d7cb6SMatthew D Fleming 				    inoinfo(inumber)->ino_state,
120623d7cb6SMatthew D Fleming 				    (uintmax_t)inumber);
121d33e92f9SJulian Elischer 			}
1228fae3551SRodney W. Grimes 		}
1238fae3551SRodney W. Grimes 	}
1248fae3551SRodney W. Grimes }
1258fae3551SRodney W. Grimes 
12631f4ab50SBruce Evans int
127b70cd7eeSWarner Losh pass4check(struct inodesc *idesc)
1288fae3551SRodney W. Grimes {
1293d438ad6SDavid E. O'Brien 	struct dups *dlp;
1308fae3551SRodney W. Grimes 	int nfrags, res = KEEPON;
1311c85e6a3SKirk McKusick 	ufs2_daddr_t blkno = idesc->id_blkno;
1328fae3551SRodney W. Grimes 
1338fae3551SRodney W. Grimes 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
1348fae3551SRodney W. Grimes 		if (chkrange(blkno, 1)) {
1358fae3551SRodney W. Grimes 			res = SKIP;
1368fae3551SRodney W. Grimes 		} else if (testbmap(blkno)) {
1378fae3551SRodney W. Grimes 			for (dlp = duplist; dlp; dlp = dlp->next) {
1388fae3551SRodney W. Grimes 				if (dlp->dup != blkno)
1398fae3551SRodney W. Grimes 					continue;
1408fae3551SRodney W. Grimes 				dlp->dup = duplist->dup;
1418fae3551SRodney W. Grimes 				dlp = duplist;
1428fae3551SRodney W. Grimes 				duplist = duplist->next;
1438fae3551SRodney W. Grimes 				free((char *)dlp);
1448fae3551SRodney W. Grimes 				break;
1458fae3551SRodney W. Grimes 			}
1467d5e6562SPedro F. Giffuni 			if (dlp == NULL) {
1478fae3551SRodney W. Grimes 				clrbmap(blkno);
1488fae3551SRodney W. Grimes 				n_blks--;
1498fae3551SRodney W. Grimes 			}
1508fae3551SRodney W. Grimes 		}
1518fae3551SRodney W. Grimes 	}
1528fae3551SRodney W. Grimes 	return (res);
1538fae3551SRodney W. Grimes }
154