xref: /openbsd-src/sbin/fsck_ext2fs/pass5.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: pass5.c,v 1.6 2001/01/31 22:31:11 deraadt Exp $	*/
2 /*	$NetBSD: pass5.c,v 1.1 1997/06/11 11:21:58 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[] = "@(#)pass5.c	8.6 (Berkeley) 11/30/94";
41 #else
42 #if 0
43 static char rcsid[] = "$NetBSD: pass5.c,v 1.1 1997/06/11 11:21:58 bouyer Exp $";
44 #else
45 static char rcsid[] = "$OpenBSD: pass5.c,v 1.6 2001/01/31 22:31:11 deraadt 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 <ufs/ext2fs/ext2fs_extern.h>
55 #include <string.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 
59 #include "fsutil.h"
60 #include "fsck.h"
61 #include "extern.h"
62 
63 
64 void print_bmap __P((u_char *,u_int32_t));
65 
66 void
67 pass5()
68 {
69 	int c;
70 	register struct m_ext2fs *fs = &sblock;
71 	daddr_t dbase, dmax;
72 	register daddr_t d;
73 	register long i, j;
74 	struct inodesc idesc[3];
75 	struct bufarea *ino_bitmap = NULL, *blk_bitmap = NULL;
76 	char *ibmap, *bbmap;
77 	u_int32_t cs_ndir, cs_nbfree, cs_nifree;
78 	char msg[255];
79 
80 	cs_ndir = 0;
81 	cs_nbfree = 0;
82 	cs_nifree = 0;
83 
84 	ibmap = malloc(fs->e2fs_bsize);
85 	bbmap = malloc(fs->e2fs_bsize);
86 	if (ibmap == NULL || bbmap == NULL) {
87 		errexit("out of memory\n");
88 	}
89 
90 	for (c = 0; c < fs->e2fs_ncg; c++) {
91 		u_int32_t nbfree = 0;
92 		u_int32_t nifree = 0;
93 		u_int32_t ndirs = 0;
94 
95 		nbfree = 0;
96 		nifree = fs->e2fs.e2fs_ipg;
97 		ndirs = 0;
98 
99 		if (blk_bitmap == NULL) {
100 			blk_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
101 				fs->e2fs_bsize);
102 		} else {
103 			getblk(blk_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
104 				fs->e2fs_bsize);
105 		}
106 		if (ino_bitmap == NULL) {
107 			ino_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
108 				fs->e2fs_bsize);
109 		} else {
110 			getblk(ino_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
111 				fs->e2fs_bsize);
112 		}
113 		memset(bbmap, 0, fs->e2fs_bsize);
114 		memset(ibmap, 0, fs->e2fs_bsize);
115 		memset(&idesc[0], 0, sizeof idesc);
116 		for (i = 0; i < 3; i++) {
117 			idesc[i].id_type = ADDR;
118 		}
119 
120 		j = fs->e2fs.e2fs_ipg * c + 1;
121 
122 		for (i = 0; i < fs->e2fs.e2fs_ipg; j++, i++) {
123 			if ((j < EXT2_FIRSTINO) && (j != EXT2_ROOTINO)) {
124 				setbit(ibmap, i);
125 				nifree--;
126 				continue;
127 			}
128 			if (j > fs->e2fs.e2fs_icount) {
129 				setbit(ibmap, i);
130 				continue;
131 			}
132 			switch (statemap[j]) {
133 
134 			case USTATE:
135 				break;
136 
137 			case DSTATE:
138 			case DCLEAR:
139 			case DFOUND:
140 				ndirs++;
141 				/* fall through */
142 
143 			case FSTATE:
144 			case FCLEAR:
145 				nifree--;
146 				setbit(ibmap, i);
147 				break;
148 
149 			default:
150 				errexit("BAD STATE %d FOR INODE I=%ld\n",
151 				    statemap[j], j);
152 			}
153 		}
154 
155 		/* fill in unused par of the inode map */
156 		for (i = fs->e2fs.e2fs_ipg / NBBY; i < fs->e2fs_bsize; i++)
157 			ibmap[i] = 0xff;
158 
159 		dbase = c * sblock.e2fs.e2fs_bpg +
160 		    sblock.e2fs.e2fs_first_dblock;
161 		dmax = (c+1) * sblock.e2fs.e2fs_bpg +
162 		    sblock.e2fs.e2fs_first_dblock;
163 
164 		for (i = 0, d = dbase;
165 		     d < dmax;
166 		     d ++, i ++) {
167 			if (testbmap(d) || d >= sblock.e2fs.e2fs_bcount) {
168 				setbit(bbmap, i);
169 				continue;
170 			} else {
171 				nbfree++;
172 			}
173 
174 		}
175 		cs_nbfree += nbfree;
176 		cs_nifree += nifree;
177 		cs_ndir += ndirs;
178 
179 		if (debug && (fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
180 					  fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
181 					  fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs)) {
182 			printf("summary info for cg %d is %d, %d, %d,"
183 					"should be %d, %d, %d\n", c,
184 					fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree),
185 					fs2h16(fs->e2fs_gd[c].ext2bgd_nifree),
186 					fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs),
187 					nbfree,
188 					nifree,
189 					ndirs);
190 		}
191 		(void)snprintf(msg, sizeof(msg),
192 		    "SUMMARY INFORMATIONS WRONG FOR CG #%d", c);
193 		if ((fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
194 			fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
195 			fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs) &&
196 			dofix(&idesc[0], msg)) {
197 			fs->e2fs_gd[c].ext2bgd_nbfree = h2fs16(nbfree);
198 			fs->e2fs_gd[c].ext2bgd_nifree = h2fs16(nifree);
199 			fs->e2fs_gd[c].ext2bgd_ndirs = h2fs16(ndirs);
200 			sbdirty();
201 		}
202 
203 		if (debug && memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize)) {
204 			printf("blk_bitmap:\n");
205 			print_bmap(blk_bitmap->b_un.b_buf, fs->e2fs_bsize);
206 			printf("bbmap:\n");
207 			print_bmap(bbmap, fs->e2fs_bsize);
208 		}
209 
210 		(void)snprintf(msg, sizeof(msg),
211 		    "BLK(S) MISSING IN BIT MAPS #%d", c);
212 		if (memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize) &&
213 			dofix(&idesc[1], msg)) {
214 			memcpy(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize);
215 			dirty(blk_bitmap);
216 		}
217 		if (debug && memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize)) {
218 			printf("ino_bitmap:\n");
219 			print_bmap(ino_bitmap->b_un.b_buf, fs->e2fs_bsize);
220 			printf("ibmap:\n");
221 			print_bmap(ibmap, fs->e2fs_bsize);
222 		}
223 		(void)snprintf(msg, sizeof(msg),
224 		    "INODE(S) MISSING IN BIT MAPS #%d", c);
225 		if (memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize) &&
226 			dofix(&idesc[1], msg)) {
227 			memcpy(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize);
228 			dirty(ino_bitmap);
229 		}
230 
231 	}
232 	if (debug && (fs->e2fs.e2fs_fbcount != cs_nbfree ||
233 		fs->e2fs.e2fs_ficount != cs_nifree)) {
234 		printf("summary info bad in superblock: %d, %d should be %d, %d\n",
235 		fs->e2fs.e2fs_fbcount, fs->e2fs.e2fs_ficount,
236 		cs_nbfree, cs_nifree);
237 	}
238 	if ((fs->e2fs.e2fs_fbcount != cs_nbfree ||
239 		fs->e2fs.e2fs_ficount != cs_nifree)
240 	    && dofix(&idesc[0], "SUPERBLK SUMMARY INFORMATION BAD")) {
241 		fs->e2fs.e2fs_fbcount = cs_nbfree;
242 		fs->e2fs.e2fs_ficount = cs_nifree;
243 		sbdirty();
244 	}
245 }
246 
247 void
248 print_bmap(map, size)
249 	u_char *map;
250 	u_int32_t size;
251 {
252 	int i, j;
253 
254 	i = 0;
255 	while (i < size) {
256 		printf("%u: ",i);
257 		for (j = 0; j < 16; j++, i++)
258 			printf("%2x ", (u_int)map[i] & 0xff);
259 		printf("\n");
260 	}
261 }
262