xref: /minix3/sbin/fsck_ext2fs/pass4.c (revision 94715d8e544a29153969034072cc27e737b55671)
1*94715d8eSBen Gras /*	$NetBSD: pass4.c,v 1.10 2009/10/19 18:41:08 bouyer Exp $	*/
2*94715d8eSBen Gras 
3*94715d8eSBen Gras /*
4*94715d8eSBen Gras  * Copyright (c) 1980, 1986, 1993
5*94715d8eSBen Gras  *	The Regents of the University of California.  All rights reserved.
6*94715d8eSBen Gras  *
7*94715d8eSBen Gras  * Redistribution and use in source and binary forms, with or without
8*94715d8eSBen Gras  * modification, are permitted provided that the following conditions
9*94715d8eSBen Gras  * are met:
10*94715d8eSBen Gras  * 1. Redistributions of source code must retain the above copyright
11*94715d8eSBen Gras  *    notice, this list of conditions and the following disclaimer.
12*94715d8eSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
13*94715d8eSBen Gras  *    notice, this list of conditions and the following disclaimer in the
14*94715d8eSBen Gras  *    documentation and/or other materials provided with the distribution.
15*94715d8eSBen Gras  * 3. Neither the name of the University nor the names of its contributors
16*94715d8eSBen Gras  *    may be used to endorse or promote products derived from this software
17*94715d8eSBen Gras  *    without specific prior written permission.
18*94715d8eSBen Gras  *
19*94715d8eSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*94715d8eSBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*94715d8eSBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*94715d8eSBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*94715d8eSBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*94715d8eSBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*94715d8eSBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*94715d8eSBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*94715d8eSBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*94715d8eSBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*94715d8eSBen Gras  * SUCH DAMAGE.
30*94715d8eSBen Gras  */
31*94715d8eSBen Gras 
32*94715d8eSBen Gras /*
33*94715d8eSBen Gras  * Copyright (c) 1997 Manuel Bouyer.
34*94715d8eSBen Gras  *
35*94715d8eSBen Gras  * Redistribution and use in source and binary forms, with or without
36*94715d8eSBen Gras  * modification, are permitted provided that the following conditions
37*94715d8eSBen Gras  * are met:
38*94715d8eSBen Gras  * 1. Redistributions of source code must retain the above copyright
39*94715d8eSBen Gras  *    notice, this list of conditions and the following disclaimer.
40*94715d8eSBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
41*94715d8eSBen Gras  *    notice, this list of conditions and the following disclaimer in the
42*94715d8eSBen Gras  *    documentation and/or other materials provided with the distribution.
43*94715d8eSBen Gras  *
44*94715d8eSBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45*94715d8eSBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46*94715d8eSBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47*94715d8eSBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48*94715d8eSBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49*94715d8eSBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50*94715d8eSBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51*94715d8eSBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52*94715d8eSBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
53*94715d8eSBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54*94715d8eSBen Gras  */
55*94715d8eSBen Gras 
56*94715d8eSBen Gras #include <sys/cdefs.h>
57*94715d8eSBen Gras #ifndef lint
58*94715d8eSBen Gras #if 0
59*94715d8eSBen Gras static char sccsid[] = "@(#)pass4.c	8.1 (Berkeley) 6/5/93";
60*94715d8eSBen Gras #else
61*94715d8eSBen Gras __RCSID("$NetBSD: pass4.c,v 1.10 2009/10/19 18:41:08 bouyer Exp $");
62*94715d8eSBen Gras #endif
63*94715d8eSBen Gras #endif /* not lint */
64*94715d8eSBen Gras 
65*94715d8eSBen Gras #include <sys/param.h>
66*94715d8eSBen Gras #include <sys/time.h>
67*94715d8eSBen Gras #include <ufs/ext2fs/ext2fs_dinode.h>
68*94715d8eSBen Gras #include <ufs/ext2fs/ext2fs.h>
69*94715d8eSBen Gras #include <stdlib.h>
70*94715d8eSBen Gras #include <string.h>
71*94715d8eSBen Gras 
72*94715d8eSBen Gras #include "fsutil.h"
73*94715d8eSBen Gras #include "fsck.h"
74*94715d8eSBen Gras #include "extern.h"
75*94715d8eSBen Gras 
76*94715d8eSBen Gras void
pass4(void)77*94715d8eSBen Gras pass4(void)
78*94715d8eSBen Gras {
79*94715d8eSBen Gras 	ino_t inumber;
80*94715d8eSBen Gras 	struct zlncnt *zlnp;
81*94715d8eSBen Gras 	struct ext2fs_dinode *dp;
82*94715d8eSBen Gras 	struct inodesc idesc;
83*94715d8eSBen Gras 	int n;
84*94715d8eSBen Gras 
85*94715d8eSBen Gras 	memset(&idesc, 0, sizeof(struct inodesc));
86*94715d8eSBen Gras 	idesc.id_type = ADDR;
87*94715d8eSBen Gras 	idesc.id_func = pass4check;
88*94715d8eSBen Gras 	for (inumber = EXT2_ROOTINO; inumber <= lastino; inumber++) {
89*94715d8eSBen Gras 		if (inumber < EXT2_FIRSTINO && inumber > EXT2_ROOTINO)
90*94715d8eSBen Gras 			continue;
91*94715d8eSBen Gras 		idesc.id_number = inumber;
92*94715d8eSBen Gras 		switch (statemap[inumber]) {
93*94715d8eSBen Gras 
94*94715d8eSBen Gras 		case FSTATE:
95*94715d8eSBen Gras 		case DFOUND:
96*94715d8eSBen Gras 			n = lncntp[inumber];
97*94715d8eSBen Gras 			if (n)
98*94715d8eSBen Gras 				adjust(&idesc, (short)n);
99*94715d8eSBen Gras 			else {
100*94715d8eSBen Gras 				for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
101*94715d8eSBen Gras 					if (zlnp->zlncnt == inumber) {
102*94715d8eSBen Gras 						zlnp->zlncnt = zlnhead->zlncnt;
103*94715d8eSBen Gras 						zlnp = zlnhead;
104*94715d8eSBen Gras 						zlnhead = zlnhead->next;
105*94715d8eSBen Gras 						free((char *)zlnp);
106*94715d8eSBen Gras 						clri(&idesc, "UNREF", 1);
107*94715d8eSBen Gras 						break;
108*94715d8eSBen Gras 					}
109*94715d8eSBen Gras 			}
110*94715d8eSBen Gras 			break;
111*94715d8eSBen Gras 
112*94715d8eSBen Gras 		case DSTATE:
113*94715d8eSBen Gras 			clri(&idesc, "UNREF", 1);
114*94715d8eSBen Gras 			break;
115*94715d8eSBen Gras 
116*94715d8eSBen Gras 		case DCLEAR:
117*94715d8eSBen Gras 			dp = ginode(inumber);
118*94715d8eSBen Gras 			if (inosize(dp) == 0) {
119*94715d8eSBen Gras 				clri(&idesc, "ZERO LENGTH", 1);
120*94715d8eSBen Gras 				break;
121*94715d8eSBen Gras 			}
122*94715d8eSBen Gras 			/* fall through */
123*94715d8eSBen Gras 		case FCLEAR:
124*94715d8eSBen Gras 			clri(&idesc, "BAD/DUP", 1);
125*94715d8eSBen Gras 			break;
126*94715d8eSBen Gras 
127*94715d8eSBen Gras 		case USTATE:
128*94715d8eSBen Gras 			break;
129*94715d8eSBen Gras 
130*94715d8eSBen Gras 		default:
131*94715d8eSBen Gras 			errexit("BAD STATE %d FOR INODE I=%llu",
132*94715d8eSBen Gras 			    statemap[inumber], (unsigned long long)inumber);
133*94715d8eSBen Gras 		}
134*94715d8eSBen Gras 	}
135*94715d8eSBen Gras }
136*94715d8eSBen Gras 
137*94715d8eSBen Gras int
pass4check(struct inodesc * idesc)138*94715d8eSBen Gras pass4check(struct inodesc *idesc)
139*94715d8eSBen Gras {
140*94715d8eSBen Gras 	struct dups *dlp;
141*94715d8eSBen Gras 	int nfrags, res = KEEPON;
142*94715d8eSBen Gras 	daddr_t blkno = idesc->id_blkno;
143*94715d8eSBen Gras 
144*94715d8eSBen Gras 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
145*94715d8eSBen Gras 		if (chkrange(blkno, 1)) {
146*94715d8eSBen Gras 			res = SKIP;
147*94715d8eSBen Gras 		} else if (testbmap(blkno)) {
148*94715d8eSBen Gras 			for (dlp = duplist; dlp; dlp = dlp->next) {
149*94715d8eSBen Gras 				if (dlp->dup != blkno)
150*94715d8eSBen Gras 					continue;
151*94715d8eSBen Gras 				dlp->dup = duplist->dup;
152*94715d8eSBen Gras 				dlp = duplist;
153*94715d8eSBen Gras 				duplist = duplist->next;
154*94715d8eSBen Gras 				free((char *)dlp);
155*94715d8eSBen Gras 				break;
156*94715d8eSBen Gras 			}
157*94715d8eSBen Gras 			if (dlp == 0) {
158*94715d8eSBen Gras 				clrbmap(blkno);
159*94715d8eSBen Gras 				n_blks--;
160*94715d8eSBen Gras 			}
161*94715d8eSBen Gras 		}
162*94715d8eSBen Gras 	}
163*94715d8eSBen Gras 	return (res);
164*94715d8eSBen Gras }
165