1 /* $OpenBSD: pass1.c,v 1.5 2000/04/26 23:26:06 jasoni Exp $ */ 2 /* $NetBSD: pass1.c,v 1.1 1997/06/11 11:21:51 bouyer Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Manuel Bouyer. 6 * Copyright (c) 1980, 1986, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)pass1.c 8.1 (Berkeley) 6/5/93"; 41 #else 42 #if 0 43 static char rcsid[] = "$NetBSD: pass1.c,v 1.1 1997/06/11 11:21:51 bouyer Exp $"; 44 #else 45 static char rcsid[] = "$OpenBSD: pass1.c,v 1.5 2000/04/26 23:26:06 jasoni Exp $"; 46 #endif 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/time.h> 52 #include <ufs/ext2fs/ext2fs_dinode.h> 53 #include <ufs/ext2fs/ext2fs_dir.h> 54 #include <ufs/ext2fs/ext2fs.h> 55 56 #include <ufs/ufs/dinode.h> /* for IFMT & friends */ 57 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <time.h> 62 63 #include "fsck.h" 64 #include "extern.h" 65 #include "fsutil.h" 66 67 static daddr_t badblk; 68 static daddr_t dupblk; 69 static void checkinode __P((ino_t, struct inodesc *)); 70 71 void 72 pass1() 73 { 74 ino_t inumber; 75 int c, i; 76 daddr_t dbase; 77 struct inodesc idesc; 78 79 /* 80 * Set file system reserved blocks in used block map. 81 */ 82 for (c = 0; c < sblock.e2fs_ncg; c++) { 83 dbase = c * sblock.e2fs.e2fs_bpg + 84 sblock.e2fs.e2fs_first_dblock; 85 /* Mark the blocks used for the inode table */ 86 if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables) >= dbase) { 87 for (i = 0; i < sblock.e2fs_itpg; i++) 88 setbmap( 89 fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables) 90 + i); 91 } 92 /* Mark the blocks used for the block bitmap */ 93 if (fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap) >= dbase) 94 setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap)); 95 /* Mark the blocks used for the inode bitmap */ 96 if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap) >= dbase) 97 setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap)); 98 99 if (sblock.e2fs.e2fs_rev == E2FS_REV0 || 100 (sblock.e2fs.e2fs_features_rocompat & 101 EXT2F_ROCOMPAT_SPARSESUPER) == 0 || 102 cg_has_sb(c)) { 103 /* Mark copuy of SB and descriptors */ 104 setbmap(dbase); 105 for (i = 1; i <= sblock.e2fs_ngdb; i++) 106 setbmap(dbase+i); 107 } 108 109 110 if (c == 0) { 111 for(i = 0; i < dbase; i++) 112 setbmap(i); 113 } 114 } 115 116 /* 117 * Find all allocated blocks. 118 */ 119 memset(&idesc, 0, sizeof(struct inodesc)); 120 idesc.id_type = ADDR; 121 idesc.id_func = pass1check; 122 inumber = 1; 123 n_files = n_blks = 0; 124 resetinodebuf(); 125 for (c = 0; c < sblock.e2fs_ncg; c++) { 126 for (i = 0; 127 i < sblock.e2fs.e2fs_ipg && inumber <= sblock.e2fs.e2fs_icount; 128 i++, inumber++) { 129 if (inumber < EXT2_ROOTINO) /* XXX */ 130 continue; 131 checkinode(inumber, &idesc); 132 } 133 } 134 freeinodebuf(); 135 } 136 137 static void 138 checkinode(inumber, idesc) 139 ino_t inumber; 140 register struct inodesc *idesc; 141 { 142 register struct ext2fs_dinode *dp; 143 struct zlncnt *zlnp; 144 int ndb, j; 145 mode_t mode; 146 147 dp = getnextinode(inumber); 148 if (inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO) 149 return; 150 151 mode = fs2h16(dp->e2di_mode) & IFMT; 152 if (mode == 0 || (dp->e2di_dtime != 0 && dp->e2di_nlink == 0)) { 153 if (mode == 0 && ( 154 memcmp(dp->e2di_blocks, zino.e2di_blocks, 155 (NDADDR + NIADDR) * sizeof(u_int32_t)) || 156 dp->e2di_mode || dp->e2di_size)) { 157 pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber); 158 if (reply("CLEAR") == 1) { 159 dp = ginode(inumber); 160 clearinode(dp); 161 inodirty(); 162 } 163 } 164 #ifdef notyet /* it seems that dtime == 0 is valid for a unallocated inode */ 165 if (dp->e2di_dtime == 0) { 166 pwarn("DELETED INODE I=%u HAS A NULL DTIME", inumber); 167 if (preen) { 168 printf(" (CORRECTED)\n"); 169 } 170 if (preen || reply("CORRECT")) { 171 time_t t; 172 time(&t); 173 dp->e2di_dtime = h2fs32(t); 174 dp = ginode(inumber); 175 inodirty(); 176 } 177 } 178 #endif 179 statemap[inumber] = USTATE; 180 return; 181 } 182 lastino = inumber; 183 if (dp->e2di_dtime != 0) { 184 time_t t = fs2h32(dp->e2di_dtime); 185 char *p = ctime(&t); 186 pwarn("INODE I=%u HAS DTIME=%12.12s %4.4s", inumber, &p[4], &p[20]); 187 if (preen) { 188 printf(" (CORRECTED)\n"); 189 } 190 if (preen || reply("CORRECT")) { 191 dp = ginode(inumber); 192 dp->e2di_dtime = 0; 193 inodirty(); 194 } 195 } 196 if (/* dp->di_size < 0 || */ 197 fs2h32(dp->e2di_size) + sblock.e2fs_bsize - 1 < 198 fs2h32(dp->e2di_size)) { 199 if (debug) 200 printf("bad size %lu:", (u_long)fs2h32(dp->e2di_size)); 201 goto unknown; 202 } 203 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) { 204 dp = ginode(inumber); 205 dp->e2di_size = h2fs32(sblock.e2fs_bsize); 206 dp->e2di_mode = h2fs16(IFREG|0600); 207 inodirty(); 208 } 209 ndb = howmany(fs2h32(dp->e2di_size), sblock.e2fs_bsize); 210 if (ndb < 0) { 211 if (debug) 212 printf("bad size %lu ndb %d:", 213 (u_long)fs2h32(dp->e2di_size), ndb); 214 goto unknown; 215 } 216 if (mode == IFBLK || mode == IFCHR) 217 ndb++; 218 if (mode == IFLNK) { 219 /* 220 * Fake ndb value so direct/indirect block checks below 221 * will detect any garbage after symlink string. 222 */ 223 if (fs2h32(dp->e2di_size) < EXT2_MAXSYMLINKLEN || 224 (EXT2_MAXSYMLINKLEN == 0 && dp->e2di_blocks == 0)) { 225 ndb = howmany(fs2h32(dp->e2di_size), sizeof(u_int32_t)); 226 if (ndb > NDADDR) { 227 j = ndb - NDADDR; 228 for (ndb = 1; j > 1; j--) 229 ndb *= NINDIR(&sblock); 230 ndb += NDADDR; 231 } 232 } 233 } 234 /* Linux puts things in blocks for FIFO, so skip this check */ 235 if (mode != IFIFO) { 236 for (j = ndb; j < NDADDR; j++) 237 if (dp->e2di_blocks[j] != 0) { 238 if (debug) 239 printf("bad direct addr: %d\n", 240 fs2h32(dp->e2di_blocks[j])); 241 goto unknown; 242 } 243 for (j = 0, ndb -= NDADDR; ndb > 0; j++) 244 ndb /= NINDIR(&sblock); 245 for (; j < NIADDR; j++) { 246 if (dp->e2di_blocks[j+NDADDR] != 0) { 247 if (debug) 248 printf("bad indirect addr: %d\n", 249 fs2h32(dp->e2di_blocks[j+NDADDR])); 250 goto unknown; 251 } 252 } 253 } 254 if (ftypeok(dp) == 0) 255 goto unknown; 256 n_files++; 257 lncntp[inumber] = fs2h16(dp->e2di_nlink); 258 if (dp->e2di_nlink == 0) { 259 zlnp = (struct zlncnt *)malloc(sizeof *zlnp); 260 if (zlnp == NULL) { 261 pfatal("LINK COUNT TABLE OVERFLOW"); 262 if (reply("CONTINUE") == 0) 263 errexit("%s\n", ""); 264 } else { 265 zlnp->zlncnt = inumber; 266 zlnp->next = zlnhead; 267 zlnhead = zlnp; 268 } 269 } 270 if (mode == IFDIR) { 271 if (dp->e2di_size == 0) 272 statemap[inumber] = DCLEAR; 273 else 274 statemap[inumber] = DSTATE; 275 cacheino(dp, inumber); 276 } else { 277 statemap[inumber] = FSTATE; 278 } 279 typemap[inumber] = E2IFTODT(mode); 280 badblk = dupblk = 0; 281 idesc->id_number = inumber; 282 (void)ckinode(dp, idesc); 283 idesc->id_entryno *= btodb(sblock.e2fs_bsize); 284 if (fs2h32(dp->e2di_nblock) != idesc->id_entryno) { 285 pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)", 286 inumber, fs2h32(dp->e2di_nblock), idesc->id_entryno); 287 if (preen) 288 printf(" (CORRECTED)\n"); 289 else if (reply("CORRECT") == 0) 290 return; 291 dp = ginode(inumber); 292 dp->e2di_nblock = h2fs32(idesc->id_entryno); 293 inodirty(); 294 } 295 return; 296 unknown: 297 pfatal("UNKNOWN FILE TYPE I=%u", inumber); 298 statemap[inumber] = FCLEAR; 299 if (reply("CLEAR") == 1) { 300 statemap[inumber] = USTATE; 301 dp = ginode(inumber); 302 clearinode(dp); 303 inodirty(); 304 } 305 } 306 307 int 308 pass1check(idesc) 309 register struct inodesc *idesc; 310 { 311 int res = KEEPON; 312 int anyout, nfrags; 313 daddr_t blkno = idesc->id_blkno; 314 register struct dups *dlp; 315 struct dups *new; 316 317 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) { 318 blkerror(idesc->id_number, "BAD", blkno); 319 if (badblk++ >= MAXBAD) { 320 pwarn("EXCESSIVE BAD BLKS I=%u", 321 idesc->id_number); 322 if (preen) 323 printf(" (SKIPPING)\n"); 324 else if (reply("CONTINUE") == 0) 325 errexit("%s\n", ""); 326 return (STOP); 327 } 328 } 329 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { 330 if (anyout && chkrange(blkno, 1)) { 331 res = SKIP; 332 } else if (!testbmap(blkno)) { 333 n_blks++; 334 setbmap(blkno); 335 } else { 336 blkerror(idesc->id_number, "DUP", blkno); 337 if (dupblk++ >= MAXDUP) { 338 pwarn("EXCESSIVE DUP BLKS I=%u", 339 idesc->id_number); 340 if (preen) 341 printf(" (SKIPPING)\n"); 342 else if (reply("CONTINUE") == 0) 343 errexit("%s\n", ""); 344 return (STOP); 345 } 346 new = (struct dups *)malloc(sizeof(struct dups)); 347 if (new == NULL) { 348 pfatal("DUP TABLE OVERFLOW."); 349 if (reply("CONTINUE") == 0) 350 errexit("%s\n", ""); 351 return (STOP); 352 } 353 new->dup = blkno; 354 if (muldup == 0) { 355 duplist = muldup = new; 356 new->next = 0; 357 } else { 358 new->next = muldup->next; 359 muldup->next = new; 360 } 361 for (dlp = duplist; dlp != muldup; dlp = dlp->next) 362 if (dlp->dup == blkno) 363 break; 364 if (dlp == muldup && dlp->dup != blkno) 365 muldup = new; 366 } 367 /* 368 * count the number of blocks found in id_entryno 369 */ 370 idesc->id_entryno++; 371 } 372 return (res); 373 } 374