1 /* 2 * Copyright (c) 1980, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #if 0 31 #ifndef lint 32 static const char sccsid[] = "@(#)pass1.c 8.6 (Berkeley) 4/28/95"; 33 #endif /* not lint */ 34 #endif 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <sys/stat.h> 40 #include <sys/sysctl.h> 41 42 #include <ufs/ufs/dinode.h> 43 #include <ufs/ufs/dir.h> 44 #include <ufs/ffs/fs.h> 45 46 #include <err.h> 47 #include <limits.h> 48 #include <stdint.h> 49 #include <string.h> 50 51 #include "fsck.h" 52 53 static ufs2_daddr_t badblk; 54 static ufs2_daddr_t dupblk; 55 static ino_t lastino; /* last inode in use */ 56 57 static int checkinode(ino_t inumber, struct inodesc *, int rebuildcg); 58 59 void 60 pass1(void) 61 { 62 struct inostat *info; 63 struct inodesc idesc; 64 ino_t inumber, inosused, mininos; 65 ufs2_daddr_t i, cgd; 66 u_int8_t *cp; 67 int c, rebuildcg; 68 69 /* 70 * Set file system reserved blocks in used block map. 71 */ 72 for (c = 0; c < sblock.fs_ncg; c++) { 73 cgd = cgdmin(&sblock, c); 74 if (c == 0) { 75 i = cgbase(&sblock, c); 76 } else 77 i = cgsblock(&sblock, c); 78 for (; i < cgd; i++) 79 setbmap(i); 80 } 81 i = sblock.fs_csaddr; 82 cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize); 83 for (; i < cgd; i++) 84 setbmap(i); 85 86 /* 87 * Find all allocated blocks. 88 */ 89 memset(&idesc, 0, sizeof(struct inodesc)); 90 idesc.id_func = pass1check; 91 n_files = n_blks = 0; 92 for (c = 0; c < sblock.fs_ncg; c++) { 93 inumber = c * sblock.fs_ipg; 94 setinodebuf(inumber); 95 getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize); 96 rebuildcg = 0; 97 if (!check_cgmagic(c, &cgrp)) 98 rebuildcg = 1; 99 if (!rebuildcg && sblock.fs_magic == FS_UFS2_MAGIC) { 100 inosused = cgrp.cg_initediblk; 101 if (inosused > sblock.fs_ipg) { 102 pfatal("%s (%d > %d) %s %d\nReset to %d\n", 103 "Too many initialized inodes", inosused, 104 sblock.fs_ipg, "in cylinder group", c, 105 sblock.fs_ipg); 106 inosused = sblock.fs_ipg; 107 } 108 } else { 109 inosused = sblock.fs_ipg; 110 } 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 if (got_sigalarm) { 118 setproctitle("%s p1 %d%%", cdevname, 119 c * 100 / sblock.fs_ncg); 120 got_sigalarm = 0; 121 } 122 /* 123 * If we are using soft updates, then we can trust the 124 * cylinder group inode allocation maps to tell us which 125 * inodes are allocated. We will scan the used inode map 126 * to find the inodes that are really in use, and then 127 * read only those inodes in from disk. 128 */ 129 if ((preen || inoopt) && usedsoftdep && !rebuildcg) { 130 cp = &cg_inosused(&cgrp)[(inosused - 1) / CHAR_BIT]; 131 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) { 132 if (*cp == 0) 133 continue; 134 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) { 135 if (*cp & i) 136 break; 137 inosused--; 138 } 139 break; 140 } 141 if (inosused < 0) 142 inosused = 0; 143 } 144 /* 145 * Allocate inoinfo structures for the allocated inodes. 146 */ 147 inostathead[c].il_numalloced = inosused; 148 if (inosused == 0) { 149 inostathead[c].il_stat = 0; 150 continue; 151 } 152 info = calloc((unsigned)inosused, sizeof(struct inostat)); 153 if (info == NULL) 154 errx(EEXIT, "cannot alloc %u bytes for inoinfo", 155 (unsigned)(sizeof(struct inostat) * inosused)); 156 inostathead[c].il_stat = info; 157 /* 158 * Scan the allocated inodes. 159 */ 160 for (i = 0; i < inosused; i++, inumber++) { 161 if (inumber < ROOTINO) { 162 (void)getnextinode(inumber, rebuildcg); 163 continue; 164 } 165 /* 166 * NULL return indicates probable end of allocated 167 * inodes during cylinder group rebuild attempt. 168 * We always keep trying until we get to the minimum 169 * valid number for this cylinder group. 170 */ 171 if (checkinode(inumber, &idesc, rebuildcg) == 0 && 172 i > cgrp.cg_initediblk) 173 break; 174 } 175 /* 176 * This optimization speeds up future runs of fsck 177 * by trimming down the number of inodes in cylinder 178 * groups that formerly had many inodes but now have 179 * fewer in use. 180 */ 181 mininos = roundup(inosused + INOPB(&sblock), INOPB(&sblock)); 182 if (inoopt && !preen && !rebuildcg && 183 sblock.fs_magic == FS_UFS2_MAGIC && 184 cgrp.cg_initediblk > 2 * INOPB(&sblock) && 185 mininos < cgrp.cg_initediblk) { 186 i = cgrp.cg_initediblk; 187 if (mininos < 2 * INOPB(&sblock)) 188 cgrp.cg_initediblk = 2 * INOPB(&sblock); 189 else 190 cgrp.cg_initediblk = mininos; 191 pwarn("CYLINDER GROUP %d: RESET FROM %ju TO %d %s\n", 192 c, i, cgrp.cg_initediblk, "VALID INODES"); 193 cgdirty(); 194 } 195 if (inosused < sblock.fs_ipg) 196 continue; 197 lastino += 1; 198 if (lastino < (c * sblock.fs_ipg)) 199 inosused = 0; 200 else 201 inosused = lastino - (c * sblock.fs_ipg); 202 if (rebuildcg && inosused > cgrp.cg_initediblk && 203 sblock.fs_magic == FS_UFS2_MAGIC) { 204 cgrp.cg_initediblk = roundup(inosused, INOPB(&sblock)); 205 pwarn("CYLINDER GROUP %d: FOUND %d VALID INODES\n", c, 206 cgrp.cg_initediblk); 207 } 208 /* 209 * If we were not able to determine in advance which inodes 210 * were in use, then reduce the size of the inoinfo structure 211 * to the size necessary to describe the inodes that we 212 * really found. 213 */ 214 if (inumber == lastino) 215 continue; 216 inostathead[c].il_numalloced = inosused; 217 if (inosused == 0) { 218 free(inostathead[c].il_stat); 219 inostathead[c].il_stat = 0; 220 continue; 221 } 222 info = calloc((unsigned)inosused, sizeof(struct inostat)); 223 if (info == NULL) 224 errx(EEXIT, "cannot alloc %u bytes for inoinfo", 225 (unsigned)(sizeof(struct inostat) * inosused)); 226 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info)); 227 free(inostathead[c].il_stat); 228 inostathead[c].il_stat = info; 229 } 230 freeinodebuf(); 231 } 232 233 static int 234 checkinode(ino_t inumber, struct inodesc *idesc, int rebuildcg) 235 { 236 union dinode *dp; 237 off_t kernmaxfilesize; 238 ufs2_daddr_t ndb; 239 mode_t mode; 240 int j, ret, offset; 241 242 if ((dp = getnextinode(inumber, rebuildcg)) == NULL) 243 return (0); 244 mode = DIP(dp, di_mode) & IFMT; 245 if (mode == 0) { 246 if ((sblock.fs_magic == FS_UFS1_MAGIC && 247 (memcmp(dp->dp1.di_db, ufs1_zino.di_db, 248 NDADDR * sizeof(ufs1_daddr_t)) || 249 memcmp(dp->dp1.di_ib, ufs1_zino.di_ib, 250 NIADDR * sizeof(ufs1_daddr_t)) || 251 dp->dp1.di_mode || dp->dp1.di_size)) || 252 (sblock.fs_magic == FS_UFS2_MAGIC && 253 (memcmp(dp->dp2.di_db, ufs2_zino.di_db, 254 NDADDR * sizeof(ufs2_daddr_t)) || 255 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib, 256 NIADDR * sizeof(ufs2_daddr_t)) || 257 dp->dp2.di_mode || dp->dp2.di_size))) { 258 pfatal("PARTIALLY ALLOCATED INODE I=%lu", 259 (u_long)inumber); 260 if (reply("CLEAR") == 1) { 261 dp = ginode(inumber); 262 clearinode(dp); 263 inodirty(); 264 } 265 } 266 inoinfo(inumber)->ino_state = USTATE; 267 return (1); 268 } 269 lastino = inumber; 270 /* This should match the file size limit in ffs_mountfs(). */ 271 if (sblock.fs_magic == FS_UFS1_MAGIC) 272 kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1; 273 else 274 kernmaxfilesize = sblock.fs_maxfilesize; 275 if (DIP(dp, di_size) > kernmaxfilesize || 276 DIP(dp, di_size) > sblock.fs_maxfilesize || 277 (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) { 278 if (debug) 279 printf("bad size %ju:", (uintmax_t)DIP(dp, di_size)); 280 goto unknown; 281 } 282 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) { 283 dp = ginode(inumber); 284 DIP_SET(dp, di_size, sblock.fs_fsize); 285 DIP_SET(dp, di_mode, IFREG|0600); 286 inodirty(); 287 } 288 if ((mode == IFBLK || mode == IFCHR || mode == IFIFO || 289 mode == IFSOCK) && DIP(dp, di_size) != 0) { 290 if (debug) 291 printf("bad special-file size %ju:", 292 (uintmax_t)DIP(dp, di_size)); 293 goto unknown; 294 } 295 if ((mode == IFBLK || mode == IFCHR) && 296 (dev_t)DIP(dp, di_rdev) == NODEV) { 297 if (debug) 298 printf("bad special-file rdev NODEV:"); 299 goto unknown; 300 } 301 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize); 302 if (ndb < 0) { 303 if (debug) 304 printf("bad size %ju ndb %ju:", 305 (uintmax_t)DIP(dp, di_size), (uintmax_t)ndb); 306 goto unknown; 307 } 308 if (mode == IFBLK || mode == IFCHR) 309 ndb++; 310 if (mode == IFLNK) { 311 /* 312 * Fake ndb value so direct/indirect block checks below 313 * will detect any garbage after symlink string. 314 */ 315 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) { 316 if (sblock.fs_magic == FS_UFS1_MAGIC) 317 ndb = howmany(DIP(dp, di_size), 318 sizeof(ufs1_daddr_t)); 319 else 320 ndb = howmany(DIP(dp, di_size), 321 sizeof(ufs2_daddr_t)); 322 if (ndb > NDADDR) { 323 j = ndb - NDADDR; 324 for (ndb = 1; j > 1; j--) 325 ndb *= NINDIR(&sblock); 326 ndb += NDADDR; 327 } 328 } 329 } 330 for (j = ndb; ndb < NDADDR && j < NDADDR; j++) 331 if (DIP(dp, di_db[j]) != 0) { 332 if (debug) 333 printf("bad direct addr[%d]: %ju\n", j, 334 (uintmax_t)DIP(dp, di_db[j])); 335 goto unknown; 336 } 337 for (j = 0, ndb -= NDADDR; ndb > 0; j++) 338 ndb /= NINDIR(&sblock); 339 for (; j < NIADDR; j++) 340 if (DIP(dp, di_ib[j]) != 0) { 341 if (debug) 342 printf("bad indirect addr: %ju\n", 343 (uintmax_t)DIP(dp, di_ib[j])); 344 goto unknown; 345 } 346 if (ftypeok(dp) == 0) 347 goto unknown; 348 n_files++; 349 inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink); 350 if (mode == IFDIR) { 351 if (DIP(dp, di_size) == 0) 352 inoinfo(inumber)->ino_state = DCLEAR; 353 else if (DIP(dp, di_nlink) <= 0) 354 inoinfo(inumber)->ino_state = DZLINK; 355 else 356 inoinfo(inumber)->ino_state = DSTATE; 357 cacheino(dp, inumber); 358 countdirs++; 359 } else if (DIP(dp, di_nlink) <= 0) 360 inoinfo(inumber)->ino_state = FZLINK; 361 else 362 inoinfo(inumber)->ino_state = FSTATE; 363 inoinfo(inumber)->ino_type = IFTODT(mode); 364 badblk = dupblk = 0; 365 idesc->id_number = inumber; 366 if (DIP(dp, di_flags) & SF_SNAPSHOT) 367 idesc->id_type = SNAP; 368 else 369 idesc->id_type = ADDR; 370 (void)ckinode(dp, idesc); 371 if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) { 372 idesc->id_type = ADDR; 373 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize); 374 for (j = 0; j < NXADDR; j++) { 375 if (--ndb == 0 && 376 (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0) 377 idesc->id_numfrags = numfrags(&sblock, 378 fragroundup(&sblock, offset)); 379 else 380 idesc->id_numfrags = sblock.fs_frag; 381 if (dp->dp2.di_extb[j] == 0) 382 continue; 383 idesc->id_blkno = dp->dp2.di_extb[j]; 384 ret = (*idesc->id_func)(idesc); 385 if (ret & STOP) 386 break; 387 } 388 } 389 if (sblock.fs_magic == FS_UFS2_MAGIC) 390 eascan(idesc, &dp->dp2); 391 idesc->id_entryno *= btodb(sblock.fs_fsize); 392 if (DIP(dp, di_blocks) != idesc->id_entryno) { 393 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)", 394 (u_long)inumber, (uintmax_t)DIP(dp, di_blocks), 395 (uintmax_t)idesc->id_entryno); 396 if (preen) 397 printf(" (CORRECTED)\n"); 398 else if (reply("CORRECT") == 0) 399 return (1); 400 if (bkgrdflag == 0) { 401 dp = ginode(inumber); 402 DIP_SET(dp, di_blocks, idesc->id_entryno); 403 inodirty(); 404 } else { 405 cmd.value = idesc->id_number; 406 cmd.size = idesc->id_entryno - DIP(dp, di_blocks); 407 if (debug) 408 printf("adjblkcnt ino %ju amount %lld\n", 409 (uintmax_t)cmd.value, (long long)cmd.size); 410 if (sysctl(adjblkcnt, MIBSIZE, 0, 0, 411 &cmd, sizeof cmd) == -1) 412 rwerror("ADJUST INODE BLOCK COUNT", cmd.value); 413 } 414 } 415 return (1); 416 unknown: 417 pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber); 418 inoinfo(inumber)->ino_state = FCLEAR; 419 if (reply("CLEAR") == 1) { 420 inoinfo(inumber)->ino_state = USTATE; 421 dp = ginode(inumber); 422 clearinode(dp); 423 inodirty(); 424 } 425 return (1); 426 } 427 428 int 429 pass1check(struct inodesc *idesc) 430 { 431 int res = KEEPON; 432 int anyout, nfrags; 433 ufs2_daddr_t blkno = idesc->id_blkno; 434 struct dups *dlp; 435 struct dups *new; 436 437 if (idesc->id_type == SNAP) { 438 if (blkno == BLK_NOCOPY) 439 return (KEEPON); 440 if (idesc->id_number == cursnapshot) { 441 if (blkno == blkstofrags(&sblock, idesc->id_lbn)) 442 return (KEEPON); 443 if (blkno == BLK_SNAP) { 444 blkno = blkstofrags(&sblock, idesc->id_lbn); 445 idesc->id_entryno -= idesc->id_numfrags; 446 } 447 } else { 448 if (blkno == BLK_SNAP) 449 return (KEEPON); 450 } 451 } 452 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) { 453 blkerror(idesc->id_number, "BAD", blkno); 454 if (badblk++ >= MAXBAD) { 455 pwarn("EXCESSIVE BAD BLKS I=%lu", 456 (u_long)idesc->id_number); 457 if (preen) 458 printf(" (SKIPPING)\n"); 459 else if (reply("CONTINUE") == 0) { 460 ckfini(0); 461 exit(EEXIT); 462 } 463 return (STOP); 464 } 465 } 466 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { 467 if (anyout && chkrange(blkno, 1)) { 468 res = SKIP; 469 } else if (!testbmap(blkno)) { 470 n_blks++; 471 setbmap(blkno); 472 } else { 473 blkerror(idesc->id_number, "DUP", blkno); 474 if (dupblk++ >= MAXDUP) { 475 pwarn("EXCESSIVE DUP BLKS I=%lu", 476 (u_long)idesc->id_number); 477 if (preen) 478 printf(" (SKIPPING)\n"); 479 else if (reply("CONTINUE") == 0) { 480 ckfini(0); 481 exit(EEXIT); 482 } 483 return (STOP); 484 } 485 new = (struct dups *)malloc(sizeof(struct dups)); 486 if (new == NULL) { 487 pfatal("DUP TABLE OVERFLOW."); 488 if (reply("CONTINUE") == 0) { 489 ckfini(0); 490 exit(EEXIT); 491 } 492 return (STOP); 493 } 494 new->dup = blkno; 495 if (muldup == 0) { 496 duplist = muldup = new; 497 new->next = 0; 498 } else { 499 new->next = muldup->next; 500 muldup->next = new; 501 } 502 for (dlp = duplist; dlp != muldup; dlp = dlp->next) 503 if (dlp->dup == blkno) 504 break; 505 if (dlp == muldup && dlp->dup != blkno) 506 muldup = new; 507 } 508 /* 509 * count the number of blocks found in id_entryno 510 */ 511 idesc->id_entryno++; 512 } 513 return (res); 514 } 515