1 /* $NetBSD: pass1.c,v 1.46 2008/10/12 23:26:12 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 #include <sys/cdefs.h> 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)pass1.c 8.6 (Berkeley) 4/28/95"; 36 #else 37 __RCSID("$NetBSD: pass1.c,v 1.46 2008/10/12 23:26:12 christos Exp $"); 38 #endif 39 #endif /* not lint */ 40 41 #include <sys/param.h> 42 #include <sys/stat.h> 43 #include <sys/time.h> 44 45 #include <ufs/ufs/dinode.h> 46 #include <ufs/ufs/dir.h> 47 #include <ufs/ffs/fs.h> 48 #include <ufs/ufs/ufs_bswap.h> 49 #include <ufs/ffs/ffs_extern.h> 50 51 #include <err.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 56 #include "fsck.h" 57 #include "extern.h" 58 #include "fsutil.h" 59 #include "exitvalues.h" 60 61 static daddr_t badblk; 62 static daddr_t dupblk; 63 static void checkinode(ino_t, struct inodesc *); 64 static ino_t lastino; 65 66 void 67 pass1(void) 68 { 69 ino_t inumber, inosused, ninosused; 70 size_t inospace; 71 int c; 72 daddr_t i, cgd; 73 struct inodesc idesc; 74 struct cg *cgp = cgrp; 75 struct inostat *info; 76 uint8_t *cp; 77 78 /* 79 * Set file system reserved blocks in used block map. 80 */ 81 for (c = 0; c < sblock->fs_ncg; c++) { 82 cgd = cgdmin(sblock, c); 83 if (c == 0) 84 i = cgbase(sblock, c); 85 else 86 i = cgsblock(sblock, c); 87 for (; i < cgd; i++) 88 setbmap(i); 89 } 90 i = sblock->fs_csaddr; 91 cgd = i + howmany(sblock->fs_cssize, sblock->fs_fsize); 92 for (; i < cgd; i++) 93 setbmap(i); 94 /* 95 * Find all allocated blocks. 96 */ 97 memset(&idesc, 0, sizeof(struct inodesc)); 98 idesc.id_func = pass1check; 99 n_files = n_blks = 0; 100 for (c = 0; c < sblock->fs_ncg; c++) { 101 inumber = c * sblock->fs_ipg; 102 setinodebuf(inumber); 103 getblk(&cgblk, cgtod(sblock, c), sblock->fs_cgsize); 104 memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize); 105 if((doswap && !needswap) || (!doswap && needswap)) 106 ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock); 107 if (is_ufs2) 108 inosused = cgp->cg_initediblk; 109 else 110 inosused = sblock->fs_ipg; 111 if (got_siginfo) { 112 printf("%s: phase 1: cyl group %d of %d (%d%%)\n", 113 cdevname(), c, sblock->fs_ncg, 114 c * 100 / sblock->fs_ncg); 115 got_siginfo = 0; 116 } 117 #ifdef PROGRESS 118 progress_bar(cdevname(), preen ? NULL : "phase 1", 119 c, sblock->fs_ncg); 120 #endif /* PROGRESS */ 121 /* 122 * If we are using soft updates, then we can trust the 123 * cylinder group inode allocation maps to tell us which 124 * inodes are allocated. We will scan the used inode map 125 * to find the inodes that are really in use, and then 126 * read only those inodes in from disk. 127 */ 128 if (preen && usedsoftdep) { 129 if (!cg_chkmagic(cgp, 0)) 130 pfatal("CG %d: BAD MAGIC NUMBER\n", c); 131 cp = &cg_inosused(cgp, 0)[(inosused - 1) / CHAR_BIT]; 132 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) { 133 if (*cp == 0) 134 continue; 135 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) { 136 if (*cp & i) 137 break; 138 inosused--; 139 } 140 break; 141 } 142 #ifdef notdef 143 if (inosused < 0) 144 inosused = 0; 145 #endif 146 } 147 /* 148 * Allocate inoinfo structures for the allocated inodes. 149 */ 150 inostathead[c].il_numalloced = inosused; 151 if (inosused == 0) { 152 inostathead[c].il_stat = 0; 153 continue; 154 } 155 inospace = inosused * sizeof(*info); 156 if (inospace / sizeof(*info) != inosused) { 157 pfatal("too many inodes %llu\n", (unsigned long long) 158 inosused); 159 exit(FSCK_EXIT_CHECK_FAILED); 160 } 161 info = malloc(inospace); 162 if (info == NULL) { 163 pfatal("cannot alloc %zu bytes for inoinfo\n", 164 inospace); 165 exit(FSCK_EXIT_CHECK_FAILED); 166 } 167 (void)memset(info, 0, inospace); 168 inostathead[c].il_stat = info; 169 /* 170 * Scan the allocated inodes. 171 */ 172 for (i = 0; i < inosused; i++, inumber++) { 173 if (inumber < ROOTINO) { 174 (void)getnextinode(inumber); 175 continue; 176 } 177 checkinode(inumber, &idesc); 178 } 179 lastino += 1; 180 if (inosused < sblock->fs_ipg || inumber == lastino) 181 continue; 182 /* 183 * If we were not able to determine in advance which inodes 184 * were in use, then reduce the size of the inoinfo structure 185 * to the size necessary to describe the inodes that we 186 * really found. 187 */ 188 if (lastino < (c * sblock->fs_ipg)) 189 ninosused = 0; 190 else 191 ninosused = lastino - (c * sblock->fs_ipg); 192 inostathead[c].il_numalloced = ninosused; 193 if (ninosused == 0) { 194 free(inostathead[c].il_stat); 195 inostathead[c].il_stat = 0; 196 continue; 197 } 198 if (ninosused != inosused) { 199 struct inostat *ninfo; 200 size_t ninospace = ninosused * sizeof(*ninfo); 201 if (ninospace / sizeof(*info) != ninosused) { 202 pfatal("too many inodes %llu\n", 203 (unsigned long long)ninosused); 204 exit(FSCK_EXIT_CHECK_FAILED); 205 } 206 ninfo = realloc(info, ninospace); 207 if (ninfo == NULL) { 208 pfatal("cannot realloc %zu bytes to %zu " 209 "for inoinfo\n", inospace, ninospace); 210 exit(FSCK_EXIT_CHECK_FAILED); 211 } 212 if (ninosused > inosused) 213 (void)memset(&ninfo[inosused], 0, ninospace - inospace); 214 inostathead[c].il_stat = ninfo; 215 } 216 } 217 #ifdef PROGRESS 218 if (!preen) 219 progress_done(); 220 #endif /* PROGRESS */ 221 freeinodebuf(); 222 do_blkswap = 0; /* has been done */ 223 } 224 225 static void 226 checkinode(ino_t inumber, struct inodesc *idesc) 227 { 228 union dinode *dp; 229 struct zlncnt *zlnp; 230 daddr_t ndb; 231 int j; 232 mode_t mode; 233 u_int64_t size, kernmaxfilesize; 234 int64_t blocks; 235 char symbuf[MAXBSIZE]; 236 struct inostat *info; 237 238 dp = getnextinode(inumber); 239 info = inoinfo(inumber); 240 mode = iswap16(DIP(dp, mode)) & IFMT; 241 size = iswap64(DIP(dp, size)); 242 if (mode == 0) { 243 if ((is_ufs2 && 244 (memcmp(dp->dp2.di_db, ufs2_zino.di_db, 245 NDADDR * sizeof(int64_t)) || 246 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib, 247 NIADDR * sizeof(int64_t)))) 248 || 249 (!is_ufs2 && 250 (memcmp(dp->dp1.di_db, ufs1_zino.di_db, 251 NDADDR * sizeof(int32_t)) || 252 memcmp(dp->dp1.di_ib, ufs1_zino.di_ib, 253 NIADDR * sizeof(int32_t)))) || 254 mode || size) { 255 pfatal("PARTIALLY ALLOCATED INODE I=%llu", 256 (unsigned long long)inumber); 257 if (reply("CLEAR") == 1) { 258 dp = ginode(inumber); 259 clearinode(dp); 260 inodirty(); 261 } else 262 markclean = 0; 263 } 264 info->ino_state = USTATE; 265 return; 266 } 267 lastino = inumber; 268 /* This should match the file size limit in ffs_mountfs(). */ 269 if (is_ufs2) 270 kernmaxfilesize = sblock->fs_maxfilesize; 271 else 272 kernmaxfilesize = (u_int64_t)0x80000000 * sblock->fs_bsize - 1; 273 if (size > kernmaxfilesize || size + sblock->fs_bsize - 1 < size || 274 (mode == IFDIR && size > MAXDIRSIZE)) { 275 if (debug) 276 printf("bad size %llu:",(unsigned long long)size); 277 goto unknown; 278 } 279 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) { 280 dp = ginode(inumber); 281 DIP_SET(dp, size, iswap64(sblock->fs_fsize)); 282 size = sblock->fs_fsize; 283 DIP_SET(dp, mode, iswap16(IFREG|0600)); 284 inodirty(); 285 } 286 ndb = howmany(size, sblock->fs_bsize); 287 if (ndb < 0) { 288 if (debug) 289 printf("bad size %llu ndb %lld:", 290 (unsigned long long)size, (long long)ndb); 291 goto unknown; 292 } 293 if (mode == IFBLK || mode == IFCHR) 294 ndb++; 295 if (mode == IFLNK) { 296 /* 297 * Note that the old fastlink format always had di_blocks set 298 * to 0. Other than that we no longer use the `spare' field 299 * (which is now the extended uid) for sanity checking, the 300 * new format is the same as the old. We simply ignore the 301 * conversion altogether. - mycroft, 19MAY1994 302 */ 303 if (!is_ufs2 && doinglevel2 && 304 size > 0 && size < MAXSYMLINKLEN_UFS1 && 305 DIP(dp, blocks) != 0) { 306 if (bread(fsreadfd, symbuf, 307 fsbtodb(sblock, iswap32(DIP(dp, db[0]))), 308 (long)secsize) != 0) 309 errexit("cannot read symlink"); 310 if (debug) { 311 symbuf[size] = 0; 312 printf("convert symlink %llu(%s) " 313 "of size %lld\n", 314 (unsigned long long)inumber, symbuf, 315 (unsigned long long)size); 316 } 317 dp = ginode(inumber); 318 memmove(dp->dp1.di_db, symbuf, (long)size); 319 DIP_SET(dp, blocks, 0); 320 inodirty(); 321 } 322 /* 323 * Fake ndb value so direct/indirect block checks below 324 * will detect any garbage after symlink string. 325 */ 326 if ((sblock->fs_maxsymlinklen < 0) || 327 (size < sblock->fs_maxsymlinklen) || 328 (isappleufs && (size < APPLEUFS_MAXSYMLINKLEN)) || 329 (sblock->fs_maxsymlinklen == 0 && DIP(dp, blocks) == 0)) { 330 if (is_ufs2) 331 ndb = howmany(size, sizeof(int64_t)); 332 else 333 ndb = howmany(size, sizeof(int32_t)); 334 if (ndb > NDADDR) { 335 j = ndb - NDADDR; 336 for (ndb = 1; j > 1; j--) 337 ndb *= NINDIR(sblock); 338 ndb += NDADDR; 339 } 340 } 341 } 342 if (ndb < NDADDR) { 343 for (j = ndb; j < NDADDR; j++) 344 if (DIP(dp, db[j]) != 0) { 345 if (debug) { 346 if (!is_ufs2) 347 printf("bad direct addr ix %d: %d [ndb %lld]\n", 348 j, iswap32(dp->dp1.di_db[j]), 349 (long long)ndb); 350 else 351 printf("bad direct addr ix %d: %lld [ndb %lld]\n", 352 j, (long long)iswap64(dp->dp2.di_db[j]), 353 (long long)ndb); 354 } 355 goto unknown; 356 } 357 } 358 359 for (j = 0, ndb -= NDADDR; ndb > 0; j++) 360 ndb /= NINDIR(sblock); 361 362 for (; j < NIADDR; j++) 363 if (DIP(dp, ib[j]) != 0) { 364 if (debug) { 365 if (!is_ufs2) 366 printf("bad indirect addr: %d\n", 367 iswap32(dp->dp1.di_ib[j])); 368 else 369 printf("bad indirect addr: %lld\n", 370 (long long)iswap64(dp->dp2.di_ib[j])); 371 } 372 goto unknown; 373 } 374 if (ftypeok(dp) == 0) 375 goto unknown; 376 n_files++; 377 info->ino_linkcnt = iswap16(DIP(dp, nlink)); 378 if (info->ino_linkcnt <= 0) { 379 zlnp = (struct zlncnt *)malloc(sizeof *zlnp); 380 if (zlnp == NULL) { 381 markclean = 0; 382 pfatal("LINK COUNT TABLE OVERFLOW"); 383 if (reply("CONTINUE") == 0) { 384 ckfini(); 385 exit(FSCK_EXIT_CHECK_FAILED); 386 } 387 } else { 388 zlnp->zlncnt = inumber; 389 zlnp->next = zlnhead; 390 zlnhead = zlnp; 391 } 392 } 393 if (mode == IFDIR) { 394 if (size == 0) 395 info->ino_state = DCLEAR; 396 else 397 info->ino_state = DSTATE; 398 cacheino(dp, inumber); 399 countdirs++; 400 } else 401 info->ino_state = FSTATE; 402 info->ino_type = IFTODT(mode); 403 if (!is_ufs2 && doinglevel2 && 404 (iswap16(dp->dp1.di_ouid) != (u_short)-1 || 405 iswap16(dp->dp1.di_ogid) != (u_short)-1)) { 406 dp = ginode(inumber); 407 dp->dp1.di_uid = iswap32(iswap16(dp->dp1.di_ouid)); 408 dp->dp1.di_ouid = iswap16(-1); 409 dp->dp1.di_gid = iswap32(iswap16(dp->dp1.di_ogid)); 410 dp->dp1.di_ogid = iswap16(-1); 411 inodirty(); 412 } 413 badblk = dupblk = 0; 414 idesc->id_number = inumber; 415 if (iswap32(DIP(dp, flags)) & SF_SNAPSHOT) 416 idesc->id_type = SNAP; 417 else 418 idesc->id_type = ADDR; 419 (void)ckinode(dp, idesc); 420 #ifdef notyet 421 if (is_ufs2 && iswap32(dp->dp2.di_extsize) > 0) { 422 int ret, offset; 423 idesc->id_type = ADDR; 424 ndb = howmany(iswap32(dp->dp2.di_extsize), sblock->fs_bsize); 425 for (j = 0; j < NXADDR; j++) { 426 if (--ndb == 0 && 427 (offset = blkoff(sblock, iswap32(dp->dp2.di_extsize))) != 0) 428 idesc->id_numfrags = numfrags(sblock, 429 fragroundup(sblock, offset)); 430 else 431 idesc->id_numfrags = sblock->fs_frag; 432 if (dp->dp2.di_extb[j] == 0) 433 continue; 434 idesc->id_blkno = iswap64(dp->dp2.di_extb[j]); 435 ret = (*idesc->id_func)(idesc); 436 if (ret & STOP) 437 break; 438 } 439 } 440 #endif 441 idesc->id_entryno *= btodb(sblock->fs_fsize); 442 if (is_ufs2) 443 blocks = iswap64(dp->dp2.di_blocks); 444 else 445 blocks = iswap32(dp->dp1.di_blocks); 446 if (blocks != idesc->id_entryno) { 447 pwarn("INCORRECT BLOCK COUNT I=%llu (%lld should be %lld)", 448 (unsigned long long)inumber, (long long)blocks, 449 (long long)idesc->id_entryno); 450 if (preen) 451 printf(" (CORRECTED)\n"); 452 else if (reply("CORRECT") == 0) { 453 markclean = 0; 454 return; 455 } 456 dp = ginode(inumber); 457 if (is_ufs2) 458 dp->dp2.di_blocks = iswap64(idesc->id_entryno); 459 else 460 dp->dp1.di_blocks = iswap32((int32_t)idesc->id_entryno); 461 inodirty(); 462 } 463 return; 464 unknown: 465 pfatal("UNKNOWN FILE TYPE I=%llu", (unsigned long long)inumber); 466 info->ino_state = FCLEAR; 467 if (reply("CLEAR") == 1) { 468 info->ino_state = USTATE; 469 dp = ginode(inumber); 470 clearinode(dp); 471 inodirty(); 472 } else 473 markclean = 0; 474 } 475 476 int 477 pass1check(struct inodesc *idesc) 478 { 479 int res = KEEPON; 480 int anyout, nfrags; 481 daddr_t blkno = idesc->id_blkno; 482 struct dups *dlp; 483 struct dups *new; 484 485 if (idesc->id_type == SNAP) { 486 if (blkno == BLK_NOCOPY || blkno == BLK_SNAP) 487 return (KEEPON); 488 } 489 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) { 490 blkerror(idesc->id_number, "BAD", blkno); 491 if (badblk++ >= MAXBAD) { 492 pwarn("EXCESSIVE BAD BLKS I=%llu", 493 (unsigned long long)idesc->id_number); 494 if (preen) 495 printf(" (SKIPPING)\n"); 496 else if (reply("CONTINUE") == 0) { 497 markclean = 0; 498 ckfini(); 499 exit(FSCK_EXIT_CHECK_FAILED); 500 } 501 return (STOP); 502 } 503 } 504 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { 505 if (anyout && chkrange(blkno, 1)) { 506 res = SKIP; 507 } else if (!testbmap(blkno)) { 508 n_blks++; 509 setbmap(blkno); 510 } else { 511 blkerror(idesc->id_number, "DUP", blkno); 512 if (dupblk++ >= MAXDUP) { 513 pwarn("EXCESSIVE DUP BLKS I=%llu", 514 (unsigned long long)idesc->id_number); 515 if (preen) 516 printf(" (SKIPPING)\n"); 517 else if (reply("CONTINUE") == 0) { 518 markclean = 0; 519 ckfini(); 520 exit(FSCK_EXIT_CHECK_FAILED); 521 } 522 return (STOP); 523 } 524 new = (struct dups *)malloc(sizeof(struct dups)); 525 if (new == NULL) { 526 markclean = 0; 527 pfatal("DUP TABLE OVERFLOW."); 528 if (reply("CONTINUE") == 0) { 529 markclean = 0; 530 ckfini(); 531 exit(FSCK_EXIT_CHECK_FAILED); 532 } 533 return (STOP); 534 } 535 new->dup = blkno; 536 if (muldup == 0) { 537 duplist = muldup = new; 538 new->next = 0; 539 } else { 540 new->next = muldup->next; 541 muldup->next = new; 542 } 543 for (dlp = duplist; dlp != muldup; dlp = dlp->next) 544 if (dlp->dup == blkno) 545 break; 546 if (dlp == muldup && dlp->dup != blkno) 547 muldup = new; 548 } 549 /* 550 * count the number of blocks found in id_entryno 551 */ 552 idesc->id_entryno++; 553 } 554 return (res); 555 } 556