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