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