xref: /freebsd/sbin/fsck_ffs/pass4.c (revision 1660ae87953868c406e94d75a0e8820248dd3067)
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.
138fae3551SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
148fae3551SRodney W. Grimes  *    must display the following acknowledgement:
158fae3551SRodney W. Grimes  *	This product includes software developed by the University of
168fae3551SRodney W. Grimes  *	California, Berkeley and its contributors.
178fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
188fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
198fae3551SRodney W. Grimes  *    without specific prior written permission.
208fae3551SRodney W. Grimes  *
218fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318fae3551SRodney W. Grimes  * SUCH DAMAGE.
328fae3551SRodney W. Grimes  */
338fae3551SRodney W. Grimes 
346b100474SJulian Elischer #if 0
35c69284caSDavid E. O'Brien #ifndef lint
36780a5c1eSPeter Wemm static const char sccsid[] = "@(#)pass4.c	8.4 (Berkeley) 4/28/95";
378fae3551SRodney W. Grimes #endif /* not lint */
38c69284caSDavid E. O'Brien #endif
39c69284caSDavid E. O'Brien #include <sys/cdefs.h>
40c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$");
418fae3551SRodney W. Grimes 
428fae3551SRodney W. Grimes #include <sys/param.h>
43780a5c1eSPeter Wemm 
448fae3551SRodney W. Grimes #include <ufs/ufs/dinode.h>
45d33e92f9SJulian Elischer #include <ufs/ffs/fs.h>
468fae3551SRodney W. Grimes 
47780a5c1eSPeter Wemm #include <err.h>
48780a5c1eSPeter Wemm #include <string.h>
49780a5c1eSPeter Wemm 
50780a5c1eSPeter Wemm #include "fsck.h"
518fae3551SRodney W. Grimes 
5231f4ab50SBruce Evans void
53b70cd7eeSWarner Losh pass4(void)
548fae3551SRodney W. Grimes {
553d438ad6SDavid E. O'Brien 	ino_t inumber;
563d438ad6SDavid E. O'Brien 	struct zlncnt *zlnp;
571c85e6a3SKirk McKusick 	union dinode *dp;
588fae3551SRodney W. Grimes 	struct inodesc idesc;
59d33e92f9SJulian Elischer 	int i, n, cg;
608fae3551SRodney W. Grimes 
61780a5c1eSPeter Wemm 	memset(&idesc, 0, sizeof(struct inodesc));
628fae3551SRodney W. Grimes 	idesc.id_type = ADDR;
638fae3551SRodney W. Grimes 	idesc.id_func = pass4check;
64d33e92f9SJulian Elischer 	for (cg = 0; cg < sblock.fs_ncg; cg++) {
656db798caSIan Dowse 		if (got_siginfo) {
666db798caSIan Dowse 			printf("%s: phase 4: cyl group %d of %d (%d%%)\n",
676db798caSIan Dowse 			    cdevname, cg, sblock.fs_ncg,
686db798caSIan Dowse 			    cg * 100 / sblock.fs_ncg);
696db798caSIan Dowse 			got_siginfo = 0;
706db798caSIan Dowse 		}
711660ae87SScott Long 		if (got_sigalarm) {
721660ae87SScott Long 			setproctitle("%s p4 %d%%", cdevname,
731660ae87SScott Long 			    cg * 100 / sblock.fs_ncg);
741660ae87SScott Long 			got_sigalarm = 0;
751660ae87SScott Long 		}
76d33e92f9SJulian Elischer 		inumber = cg * sblock.fs_ipg;
77d33e92f9SJulian Elischer 		for (i = 0; i < inostathead[cg].il_numalloced; i++, inumber++) {
78d33e92f9SJulian Elischer 			if (inumber < ROOTINO)
79d33e92f9SJulian Elischer 				continue;
808fae3551SRodney W. Grimes 			idesc.id_number = inumber;
81d33e92f9SJulian Elischer 			switch (inoinfo(inumber)->ino_state) {
828fae3551SRodney W. Grimes 
838fae3551SRodney W. Grimes 			case FSTATE:
848fae3551SRodney W. Grimes 			case DFOUND:
85d33e92f9SJulian Elischer 				n = inoinfo(inumber)->ino_linkcnt;
86d33e92f9SJulian Elischer 				if (n) {
878fae3551SRodney W. Grimes 					adjust(&idesc, (short)n);
88d33e92f9SJulian Elischer 					break;
89d33e92f9SJulian Elischer 				}
90d33e92f9SJulian Elischer 				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) {
918fae3551SRodney W. Grimes 					if (zlnp->zlncnt == inumber) {
928fae3551SRodney W. Grimes 						zlnp->zlncnt = zlnhead->zlncnt;
938fae3551SRodney W. Grimes 						zlnp = zlnhead;
948fae3551SRodney W. Grimes 						zlnhead = zlnhead->next;
958fae3551SRodney W. Grimes 						free((char *)zlnp);
968fae3551SRodney W. Grimes 						clri(&idesc, "UNREF", 1);
978fae3551SRodney W. Grimes 						break;
988fae3551SRodney W. Grimes 					}
998fae3551SRodney W. Grimes 				}
1008fae3551SRodney W. Grimes 				break;
1018fae3551SRodney W. Grimes 
1028fae3551SRodney W. Grimes 			case DSTATE:
1038fae3551SRodney W. Grimes 				clri(&idesc, "UNREF", 1);
1048fae3551SRodney W. Grimes 				break;
1058fae3551SRodney W. Grimes 
1068fae3551SRodney W. Grimes 			case DCLEAR:
1078fae3551SRodney W. Grimes 				dp = ginode(inumber);
1081c85e6a3SKirk McKusick 				if (DIP(dp, di_size) == 0) {
1098fae3551SRodney W. Grimes 					clri(&idesc, "ZERO LENGTH", 1);
1108fae3551SRodney W. Grimes 					break;
1118fae3551SRodney W. Grimes 				}
1128fae3551SRodney W. Grimes 				/* fall through */
1138fae3551SRodney W. Grimes 			case FCLEAR:
1148fae3551SRodney W. Grimes 				clri(&idesc, "BAD/DUP", 1);
1158fae3551SRodney W. Grimes 				break;
1168fae3551SRodney W. Grimes 
1178fae3551SRodney W. Grimes 			case USTATE:
1188fae3551SRodney W. Grimes 				break;
1198fae3551SRodney W. Grimes 
1208fae3551SRodney W. Grimes 			default:
121780a5c1eSPeter Wemm 				errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
122d33e92f9SJulian Elischer 				    inoinfo(inumber)->ino_state, inumber);
123d33e92f9SJulian Elischer 			}
1248fae3551SRodney W. Grimes 		}
1258fae3551SRodney W. Grimes 	}
1268fae3551SRodney W. Grimes }
1278fae3551SRodney W. Grimes 
12831f4ab50SBruce Evans int
129b70cd7eeSWarner Losh pass4check(struct inodesc *idesc)
1308fae3551SRodney W. Grimes {
1313d438ad6SDavid E. O'Brien 	struct dups *dlp;
1328fae3551SRodney W. Grimes 	int nfrags, res = KEEPON;
1331c85e6a3SKirk McKusick 	ufs2_daddr_t blkno = idesc->id_blkno;
1348fae3551SRodney W. Grimes 
1358fae3551SRodney W. Grimes 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
1368fae3551SRodney W. Grimes 		if (chkrange(blkno, 1)) {
1378fae3551SRodney W. Grimes 			res = SKIP;
1388fae3551SRodney W. Grimes 		} else if (testbmap(blkno)) {
1398fae3551SRodney W. Grimes 			for (dlp = duplist; dlp; dlp = dlp->next) {
1408fae3551SRodney W. Grimes 				if (dlp->dup != blkno)
1418fae3551SRodney W. Grimes 					continue;
1428fae3551SRodney W. Grimes 				dlp->dup = duplist->dup;
1438fae3551SRodney W. Grimes 				dlp = duplist;
1448fae3551SRodney W. Grimes 				duplist = duplist->next;
1458fae3551SRodney W. Grimes 				free((char *)dlp);
1468fae3551SRodney W. Grimes 				break;
1478fae3551SRodney W. Grimes 			}
1488fae3551SRodney W. Grimes 			if (dlp == 0) {
1498fae3551SRodney W. Grimes 				clrbmap(blkno);
1508fae3551SRodney W. Grimes 				n_blks--;
1518fae3551SRodney W. Grimes 			}
1528fae3551SRodney W. Grimes 		}
1538fae3551SRodney W. Grimes 	}
1548fae3551SRodney W. Grimes 	return (res);
1558fae3551SRodney W. Grimes }
156