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