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