1 /* $NetBSD: pass5.c,v 1.19 2011/08/06 16:42:41 dholland Exp $ */
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 * Copyright (c) 1997 Manuel Bouyer.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
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.
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.
56 #include <sys/cdefs.h>
59 static char sccsid
[] = "@(#)pass5.c 8.6 (Berkeley) 11/30/94";
61 __RCSID("$NetBSD: pass5.c,v 1.19 2011/08/06 16:42:41 dholland Exp $");
65 #include <sys/param.h>
67 #include <ufs/ufs/dinode.h>
68 #include <ufs/ext2fs/ext2fs_dinode.h>
69 #include <ufs/ext2fs/ext2fs.h>
80 static void print_bmap(char *, uint32_t);
86 struct m_ext2fs
*fs
= &sblock
;
90 struct inodesc idesc
[3];
91 struct bufarea
*ino_bitmap
= NULL
, *blk_bitmap
= NULL
;
93 u_int32_t cs_ndir
, cs_nbfree
, cs_nifree
;
100 ibmap
= malloc(fs
->e2fs_bsize
);
101 bbmap
= malloc(fs
->e2fs_bsize
);
102 if (ibmap
== NULL
|| bbmap
== NULL
) {
103 errexit("out of memory");
106 for (c
= 0; c
< fs
->e2fs_ncg
; c
++) {
107 u_int32_t nbfree
= 0;
108 u_int32_t nifree
= 0;
112 nifree
= fs
->e2fs
.e2fs_ipg
;
115 if (blk_bitmap
== NULL
) {
116 blk_bitmap
= getdatablk(fs2h32(fs
->e2fs_gd
[c
].ext2bgd_b_bitmap
),
119 getblk(blk_bitmap
, fs2h32(fs
->e2fs_gd
[c
].ext2bgd_b_bitmap
),
122 if (ino_bitmap
== NULL
) {
123 ino_bitmap
= getdatablk(fs2h32(fs
->e2fs_gd
[c
].ext2bgd_i_bitmap
),
126 getblk(ino_bitmap
, fs2h32(fs
->e2fs_gd
[c
].ext2bgd_i_bitmap
),
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
;
136 j
= fs
->e2fs
.e2fs_ipg
* c
+ 1;
138 for (i
= 0; i
< fs
->e2fs
.e2fs_ipg
; j
++, i
++) {
139 if ((j
< EXT2_FIRSTINO
) && (j
!= EXT2_ROOTINO
)) {
144 if (j
> fs
->e2fs
.e2fs_icount
) {
148 switch (statemap
[j
]) {
166 errexit("BAD STATE %d FOR INODE I=%"PRIu32
,
171 /* fill in unused par of the inode map */
172 for (i
= fs
->e2fs
.e2fs_ipg
/ NBBY
; i
< (uint32_t)fs
->e2fs_bsize
; i
++)
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
;
180 for (i
= 0, d
= dbase
;
183 if (testbmap(d
) || d
>= sblock
.e2fs
.e2fs_bcount
) {
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
),
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
);
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
);
223 print_bmap(bbmap
, fs
->e2fs_bsize
);
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
);
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
);
237 print_bmap(ibmap
, fs
->e2fs_bsize
);
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
);
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
);
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
;
266 print_bmap(char *map
, uint32_t size
)
273 for (j
= 0; j
< 16; j
++, i
++)
274 printf("%02x ", (unsigned char)map
[i
] & 0xff);