pass1.c (12567B)
1 /* $NetBSD: pass1.c,v 1.26 2017/04/21 19:33:56 christos 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[] = "@(#)pass1.c 8.1 (Berkeley) 6/5/93"; 59 #else 60 __RCSID("$NetBSD: pass1.c,v 1.26 2017/04/21 19:33:56 christos 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_dir.h> 68 #include <ufs/ext2fs/ext2fs.h> 69 70 #include <stdio.h> 71 #include <stdlib.h> 72 #include <string.h> 73 #include <time.h> 74 75 #include "fsck.h" 76 #include "extern.h" 77 #include "fsutil.h" 78 #include "exitvalues.h" 79 80 static daddr_t badblk; 81 static daddr_t dupblk; 82 static void checkinode(ino_t, struct inodesc *); 83 84 void 85 pass1(void) 86 { 87 ino_t inumber; 88 int c, i; 89 size_t j; 90 daddr_t dbase; 91 struct inodesc idesc; 92 93 /* 94 * Set file system reserved blocks in used block map. 95 */ 96 for (c = 0; c < sblock.e2fs_ncg; c++) { 97 dbase = c * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock; 98 /* Mark the blocks used for the inode table */ 99 if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables) >= dbase) { 100 for (i = 0; i < sblock.e2fs_itpg; i++) 101 setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables) + i); 102 } 103 /* Mark the blocks used for the block bitmap */ 104 if (fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap) >= dbase) 105 setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap)); 106 /* Mark the blocks used for the inode bitmap */ 107 if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap) >= dbase) 108 setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap)); 109 110 if (sblock.e2fs.e2fs_rev == E2FS_REV0 111 || (sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_SPARSESUPER) == 0 || cg_has_sb(c)) { 112 /* Mark copuy of SB and descriptors */ 113 setbmap(dbase); 114 for (i = 1; i <= sblock.e2fs_ngdb; i++) 115 setbmap(dbase + i); 116 } 117 118 if (c == 0) { 119 for (i = 0; i < dbase; i++) 120 setbmap(i); 121 } 122 } 123 124 /* 125 * Find all allocated blocks. 126 */ 127 memset(&idesc, 0, sizeof(struct inodesc)); 128 idesc.id_type = ADDR; 129 idesc.id_func = pass1check; 130 inumber = 1; 131 n_files = n_blks = 0; 132 resetinodebuf(); 133 for (c = 0; c < sblock.e2fs_ncg; c++) { 134 for (j = 0; j < sblock.e2fs.e2fs_ipg && inumber <= sblock.e2fs.e2fs_icount; j++, inumber++) { 135 if (inumber < EXT2_ROOTINO) 136 continue; 137 checkinode(inumber, &idesc); 138 } 139 } 140 freeinodebuf(); 141 } 142 143 static void 144 checkinode(ino_t inumber, struct inodesc *idesc) 145 { 146 struct ext2fs_dinode *dp; 147 struct zlncnt *zlnp; 148 int ndb, j; 149 mode_t mode; 150 151 dp = getnextinode(inumber); 152 if (inumber == 8 && (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_HASJOURNAL)) { 153 if ((fs2h16(dp->e2di_mode) & IFMT) != IFREG) 154 errexit("invalid journal inode"); 155 idesc->id_number = inumber; 156 ckinode(dp, idesc); 157 return; 158 } 159 if (inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO 160 && !( 161 inumber == EXT2_RESIZEINO && (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0 162 )) 163 return; 164 165 if (dp->e2di_mode && (dp->e2di_facl || dp->e2di_facl_high)) 166 errexit("inode %llu has an external attribute block", (unsigned long long)inumber); 167 168 mode = fs2h16(dp->e2di_mode) & IFMT; 169 if (mode == 0 || (dp->e2di_dtime != 0 && dp->e2di_nlink == 0)) { 170 if (mode == 0 171 && (memcmp( 172 dp->e2di_blocks, 173 zino.e2di_blocks, 174 (EXT2FS_NDADDR + EXT2FS_NIADDR) * sizeof(u_int32_t) 175 ) 176 || dp->e2di_mode || inosize(dp))) { 177 pfatal("PARTIALLY ALLOCATED INODE I=%llu", (unsigned long long)inumber); 178 if (reply("CLEAR") == 1) { 179 dp = ginode(inumber); 180 clearinode(dp); 181 inodirty(); 182 } 183 } 184 #ifdef notyet /* it seems that dtime == 0 is valid for a unallocated inode */ 185 if (dp->e2di_dtime == 0) { 186 pwarn("DELETED INODE I=%llu HAS A NULL DTIME", (unsigned long long)inumber); 187 if (preen) { 188 printf(" (CORRECTED)\n"); 189 } 190 if (preen || reply("CORRECT")) { 191 time_t t; 192 time(&t); 193 dp->e2di_dtime = h2fs32(t); 194 dp = ginode(inumber); 195 inodirty(); 196 } 197 } 198 #endif 199 statemap[inumber] = USTATE; 200 return; 201 } 202 lastino = inumber; 203 if (dp->e2di_dtime != 0) { 204 pwarn( 205 "INODE I=%llu HAS DTIME=%s", 206 (unsigned long long)inumber, 207 print_mtime(fs2h32(dp->e2di_dtime)) 208 ); 209 if (preen) { 210 printf(" (CORRECTED)\n"); 211 } 212 if (preen || reply("CORRECT")) { 213 dp = ginode(inumber); 214 dp->e2di_dtime = 0; 215 inodirty(); 216 } 217 } 218 if (inosize(dp) + sblock.e2fs_bsize - 1 < inosize(dp)) { 219 if (debug) 220 printf("bad size %llu:", (unsigned long long)inosize(dp)); 221 goto unknown; 222 } 223 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) { 224 dp = ginode(inumber); 225 dp->e2di_mode = h2fs16(IFREG | 0600); 226 inossize(dp, sblock.e2fs_bsize); 227 inodirty(); 228 } 229 ndb = howmany(inosize(dp), sblock.e2fs_bsize); 230 if (ndb < 0) { 231 if (debug) 232 printf("bad size %llu ndb %d:", (unsigned long long)inosize(dp), ndb); 233 goto unknown; 234 } 235 if (mode == IFBLK || mode == IFCHR) 236 ndb++; 237 if (mode == IFLNK) { 238 /* 239 * Fake ndb value so direct/indirect block checks below 240 * will detect any garbage after symlink string. 241 */ 242 if (inosize(dp) < EXT2_MAXSYMLINKLEN || (EXT2_MAXSYMLINKLEN == 0 && inonblock(dp) == 0)) { 243 ndb = howmany(inosize(dp), sizeof(u_int32_t)); 244 if (ndb > EXT2FS_NDADDR) { 245 j = ndb - EXT2FS_NDADDR; 246 for (ndb = 1; j > 1; j--) 247 ndb *= EXT2_NINDIR(&sblock); 248 ndb += EXT2FS_NDADDR; 249 } 250 } 251 } 252 /* Linux puts things in blocks for FIFO, so skip this check */ 253 if (mode != IFIFO) { 254 for (j = ndb; j < EXT2FS_NDADDR; j++) 255 if (dp->e2di_blocks[j] != 0) { 256 if (debug) 257 printf("bad direct addr: %d\n", fs2h32(dp->e2di_blocks[j])); 258 goto unknown; 259 } 260 for (j = 0, ndb -= EXT2FS_NDADDR; ndb > 0; j++) 261 ndb /= EXT2_NINDIR(&sblock); 262 for (; j < EXT2FS_NIADDR; j++) { 263 if (dp->e2di_blocks[j + EXT2FS_NDADDR] != 0) { 264 if (debug) 265 printf("bad indirect addr: %d\n", fs2h32(dp->e2di_blocks[j + EXT2FS_NDADDR])); 266 goto unknown; 267 } 268 } 269 } 270 if (ftypeok(dp) == 0) 271 goto unknown; 272 if (inumber >= EXT2_FIRSTINO || inumber == EXT2_ROOTINO) { 273 /* Don't count reserved inodes except root */ 274 n_files++; 275 } 276 lncntp[inumber] = fs2h16(dp->e2di_nlink); 277 if (dp->e2di_nlink == 0) { 278 zlnp = malloc(sizeof *zlnp); 279 if (zlnp == NULL) { 280 pfatal("LINK COUNT TABLE OVERFLOW"); 281 if (reply("CONTINUE") == 0) 282 exit(FSCK_EXIT_CHECK_FAILED); 283 } else { 284 zlnp->zlncnt = inumber; 285 zlnp->next = zlnhead; 286 zlnhead = zlnp; 287 } 288 } 289 if (mode == IFDIR) { 290 if (inosize(dp) == 0) 291 statemap[inumber] = DCLEAR; 292 else 293 statemap[inumber] = DSTATE; 294 cacheino(dp, inumber); 295 } else { 296 statemap[inumber] = FSTATE; 297 } 298 typemap[inumber] = E2IFTODT(mode); 299 badblk = dupblk = 0; 300 idesc->id_number = inumber; 301 (void)ckinode(dp, idesc); 302 idesc->id_entryno *= btodb(sblock.e2fs_bsize); 303 if (inonblock(dp) != (uint32_t)idesc->id_entryno) { 304 pwarn( 305 "INCORRECT BLOCK COUNT I=%llu (%llu should be %d)", 306 (unsigned long long)inumber, 307 (unsigned long long)inonblock(dp), 308 idesc->id_entryno 309 ); 310 if (preen) 311 printf(" (CORRECTED)\n"); 312 else if (reply("CORRECT") == 0) 313 return; 314 dp = ginode(inumber); 315 inosnblock(dp, idesc->id_entryno); 316 inodirty(); 317 } 318 return; 319 unknown: 320 pfatal("UNKNOWN FILE TYPE I=%llu", (unsigned long long)inumber); 321 statemap[inumber] = FCLEAR; 322 if (reply("CLEAR") == 1) { 323 statemap[inumber] = USTATE; 324 dp = ginode(inumber); 325 clearinode(dp); 326 inodirty(); 327 } 328 } 329 330 int 331 pass1check(struct inodesc *idesc) 332 { 333 int res = KEEPON; 334 int anyout, nfrags; 335 daddr_t blkno = idesc->id_blkno; 336 struct dups *dlp; 337 struct dups *new; 338 339 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) { 340 blkerror(idesc->id_number, "BAD", blkno); 341 if (badblk++ >= MAXBAD) { 342 pwarn("EXCESSIVE BAD BLKS I=%llu", (unsigned long long)idesc->id_number); 343 if (preen) 344 printf(" (SKIPPING)\n"); 345 else if (reply("CONTINUE") == 0) 346 exit(FSCK_EXIT_CHECK_FAILED); 347 return (STOP); 348 } 349 } 350 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { 351 if (anyout && chkrange(blkno, 1)) { 352 res = SKIP; 353 } else if (!testbmap(blkno)) { 354 n_blks++; 355 setbmap(blkno); 356 } else { 357 blkerror(idesc->id_number, "DUP", blkno); 358 if (dupblk++ >= MAXDUP) { 359 pwarn("EXCESSIVE DUP BLKS I=%llu", (unsigned long long)idesc->id_number); 360 if (preen) 361 printf(" (SKIPPING)\n"); 362 else if (reply("CONTINUE") == 0) 363 exit(FSCK_EXIT_CHECK_FAILED); 364 return (STOP); 365 } 366 new = malloc(sizeof(struct dups)); 367 if (new == NULL) { 368 pfatal("DUP TABLE OVERFLOW."); 369 if (reply("CONTINUE") == 0) 370 exit(FSCK_EXIT_CHECK_FAILED); 371 return (STOP); 372 } 373 new->dup = blkno; 374 if (muldup == 0) { 375 duplist = muldup = new; 376 new->next = 0; 377 } else { 378 new->next = muldup->next; 379 muldup->next = new; 380 } 381 for (dlp = duplist; dlp != muldup; dlp = dlp->next) 382 if (dlp->dup == blkno) 383 break; 384 if (dlp == muldup && dlp->dup != blkno) 385 muldup = new; 386 } 387 /* 388 * count the number of blocks found in id_entryno 389 */ 390 idesc->id_entryno++; 391 } 392 return (res); 393 }