xref: /netbsd/sbin/fsck_ext2fs/pass5.c (revision bf9ec67e)
1 /*	$NetBSD: pass5.c,v 1.7 2000/01/28 16:01:46 bouyer Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Manuel Bouyer.
5  * Copyright (c) 1980, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)pass5.c	8.6 (Berkeley) 11/30/94";
41 #else
42 __RCSID("$NetBSD: pass5.c,v 1.7 2000/01/28 16:01:46 bouyer Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ext2fs/ext2fs_dinode.h>
50 #include <ufs/ext2fs/ext2fs.h>
51 #include <ufs/ext2fs/ext2fs_extern.h>
52 #include <string.h>
53 #include <malloc.h>
54 #include <stdio.h>
55 
56 #include "fsutil.h"
57 #include "fsck.h"
58 #include "extern.h"
59 
60 
61 void print_bmap __P((u_char *,u_int32_t));
62 
63 void
64 pass5()
65 {
66 	int c;
67 	struct m_ext2fs *fs = &sblock;
68 	daddr_t dbase, dmax;
69 	daddr_t d;
70 	long i, j;
71 	struct inodesc idesc[3];
72 	struct bufarea *ino_bitmap = NULL, *blk_bitmap = NULL;
73 	char *ibmap, *bbmap;
74 	u_int32_t cs_ndir, cs_nbfree, cs_nifree;
75 	char msg[255];
76 
77 	cs_ndir = 0;
78 	cs_nbfree = 0;
79 	cs_nifree = 0;
80 
81 	ibmap = malloc(fs->e2fs_bsize);
82 	bbmap = malloc(fs->e2fs_bsize);
83 	if (ibmap == NULL || bbmap == NULL) {
84 		errexit("out of memory\n");
85 	}
86 
87 	for (c = 0; c < fs->e2fs_ncg; c++) {
88 		u_int32_t nbfree = 0;
89 		u_int32_t nifree = 0;
90 		u_int32_t ndirs = 0;
91 
92 		nbfree = 0;
93 		nifree = fs->e2fs.e2fs_ipg;
94 		ndirs = 0;
95 
96 		if (blk_bitmap == NULL) {
97 			blk_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
98 				fs->e2fs_bsize);
99 		} else {
100 			getblk(blk_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap),
101 				fs->e2fs_bsize);
102 		}
103 		if (ino_bitmap == NULL) {
104 			ino_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
105 				fs->e2fs_bsize);
106 		} else {
107 			getblk(ino_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap),
108 				fs->e2fs_bsize);
109 		}
110 		memset(bbmap, 0, fs->e2fs_bsize);
111 		memset(ibmap, 0, fs->e2fs_bsize);
112 		memset(&idesc[0], 0, sizeof idesc);
113 		for (i = 0; i < 3; i++) {
114 			idesc[i].id_type = ADDR;
115 		}
116 
117 		j = fs->e2fs.e2fs_ipg * c + 1;
118 
119 		for (i = 0; i < fs->e2fs.e2fs_ipg; j++, i++) {
120 			if ((j < EXT2_FIRSTINO) && (j != EXT2_ROOTINO)) {
121 				setbit(ibmap, i);
122 				nifree--;
123 				continue;
124 			}
125 			if (j > fs->e2fs.e2fs_icount) {
126 				setbit(ibmap, i);
127 				continue;
128 			}
129 			switch (statemap[j]) {
130 
131 			case USTATE:
132 				break;
133 
134 			case DSTATE:
135 			case DCLEAR:
136 			case DFOUND:
137 				ndirs++;
138 				/* fall through */
139 
140 			case FSTATE:
141 			case FCLEAR:
142 				nifree--;
143 				setbit(ibmap, i);
144 				break;
145 
146 			default:
147 				errexit("BAD STATE %d FOR INODE I=%ld\n",
148 				    statemap[j], j);
149 			}
150 		}
151 
152 		/* fill in unused par of the inode map */
153 		for (i = fs->e2fs.e2fs_ipg / NBBY; i < fs->e2fs_bsize; i++)
154 			ibmap[i] = 0xff;
155 
156 		dbase = c * sblock.e2fs.e2fs_bpg +
157 		    sblock.e2fs.e2fs_first_dblock;
158 		dmax = (c+1) * sblock.e2fs.e2fs_bpg +
159 		    sblock.e2fs.e2fs_first_dblock;
160 
161 		for (i = 0, d = dbase;
162 		     d < dmax;
163 		     d ++, i ++) {
164 			if (testbmap(d) || d >= sblock.e2fs.e2fs_bcount) {
165 				setbit(bbmap, i);
166 				continue;
167 			} else {
168 				nbfree++;
169 			}
170 
171 		}
172 		cs_nbfree += nbfree;
173 		cs_nifree += nifree;
174 		cs_ndir += ndirs;
175 
176 		if (debug && (fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
177 					  fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
178 					  fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs)) {
179 			printf("summary info for cg %d is %d, %d, %d,"
180 					"should be %d, %d, %d\n", c,
181 					fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree),
182 					fs2h16(fs->e2fs_gd[c].ext2bgd_nifree),
183 					fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs),
184 					nbfree,
185 					nifree,
186 					ndirs);
187 		}
188 		(void)snprintf(msg, sizeof(msg),
189 		    "SUMMARY INFORMATIONS WRONG FOR CG #%d", c);
190 		if ((fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree ||
191 			fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree ||
192 			fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs) &&
193 			dofix(&idesc[0], msg)) {
194 			fs->e2fs_gd[c].ext2bgd_nbfree = h2fs16(nbfree);
195 			fs->e2fs_gd[c].ext2bgd_nifree = h2fs16(nifree);
196 			fs->e2fs_gd[c].ext2bgd_ndirs = h2fs16(ndirs);
197 			sbdirty();
198 		}
199 
200 		if (debug && memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize)) {
201 			printf("blk_bitmap:\n");
202 			print_bmap(blk_bitmap->b_un.b_buf, fs->e2fs_bsize);
203 			printf("bbmap:\n");
204 			print_bmap(bbmap, fs->e2fs_bsize);
205 		}
206 
207 		(void)snprintf(msg, sizeof(msg),
208 		    "BLK(S) MISSING IN BIT MAPS #%d", c);
209 		if (memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize) &&
210 			dofix(&idesc[1], msg)) {
211 			memcpy(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize);
212 			dirty(blk_bitmap);
213 		}
214 		if (debug && memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize)) {
215 			printf("ino_bitmap:\n");
216 			print_bmap(ino_bitmap->b_un.b_buf, fs->e2fs_bsize);
217 			printf("ibmap:\n");
218 			print_bmap(ibmap, fs->e2fs_bsize);
219 		}
220 		(void)snprintf(msg, sizeof(msg),
221 		    "INODE(S) MISSING IN BIT MAPS #%d", c);
222 		if (memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize) &&
223 			dofix(&idesc[1], msg)) {
224 			memcpy(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize);
225 			dirty(ino_bitmap);
226 		}
227 
228 	}
229 	if (debug && (fs->e2fs.e2fs_fbcount != cs_nbfree ||
230 		fs->e2fs.e2fs_ficount != cs_nifree)) {
231 		printf("summary info bad in superblock: %d, %d should be %d, %d\n",
232 		fs->e2fs.e2fs_fbcount, fs->e2fs.e2fs_ficount,
233 		cs_nbfree, cs_nifree);
234 	}
235 	if ((fs->e2fs.e2fs_fbcount != cs_nbfree ||
236 		fs->e2fs.e2fs_ficount != cs_nifree)
237 	    && dofix(&idesc[0], "SUPERBLK SUMMARY INFORMATION BAD")) {
238 		fs->e2fs.e2fs_fbcount = cs_nbfree;
239 		fs->e2fs.e2fs_ficount = cs_nifree;
240 		sbdirty();
241 	}
242 }
243 
244 void
245 print_bmap(map, size)
246 	u_char *map;
247 	u_int32_t size;
248 {
249 	int i, j;
250 
251 	i = 0;
252 	while (i < size) {
253 		printf("%u: ",i);
254 		for (j = 0; j < 16; j++, i++)
255 			printf("%2x ", (u_int)map[i] & 0xff);
256 		printf("\n");
257 	}
258 }
259