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