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