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 void checkinode(ino_t inumber, struct inodesc *); 58 59 void 60 pass1(void) 61 { 62 struct inostat *info; 63 struct inodesc idesc; 64 ino_t inumber, inosused; 65 ufs2_daddr_t i, cgd; 66 u_int8_t *cp; 67 int c; 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 if (sblock.fs_magic == FS_UFS2_MAGIC) 97 inosused = cgrp.cg_initediblk; 98 else 99 inosused = sblock.fs_ipg; 100 if (got_siginfo) { 101 printf("%s: phase 1: cyl group %d of %d (%d%%)\n", 102 cdevname, c, sblock.fs_ncg, 103 c * 100 / sblock.fs_ncg); 104 got_siginfo = 0; 105 } 106 if (got_sigalarm) { 107 setproctitle("%s p1 %d%%", cdevname, 108 c * 100 / sblock.fs_ncg); 109 got_sigalarm = 0; 110 } 111 /* 112 * If we are using soft updates, then we can trust the 113 * cylinder group inode allocation maps to tell us which 114 * inodes are allocated. We will scan the used inode map 115 * to find the inodes that are really in use, and then 116 * read only those inodes in from disk. 117 */ 118 if (preen && usedsoftdep) { 119 if (!cg_chkmagic(&cgrp)) 120 pfatal("CG %d: BAD MAGIC NUMBER\n", c); 121 cp = &cg_inosused(&cgrp)[(inosused - 1) / CHAR_BIT]; 122 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) { 123 if (*cp == 0) 124 continue; 125 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) { 126 if (*cp & i) 127 break; 128 inosused--; 129 } 130 break; 131 } 132 if (inosused < 0) 133 inosused = 0; 134 } 135 /* 136 * Allocate inoinfo structures for the allocated inodes. 137 */ 138 inostathead[c].il_numalloced = inosused; 139 if (inosused == 0) { 140 inostathead[c].il_stat = 0; 141 continue; 142 } 143 info = calloc((unsigned)inosused, sizeof(struct inostat)); 144 if (info == NULL) 145 errx(EEXIT, "cannot alloc %u bytes for inoinfo", 146 (unsigned)(sizeof(struct inostat) * inosused)); 147 inostathead[c].il_stat = info; 148 /* 149 * Scan the allocated inodes. 150 */ 151 for (i = 0; i < inosused; i++, inumber++) { 152 if (inumber < ROOTINO) { 153 (void)getnextinode(inumber); 154 continue; 155 } 156 checkinode(inumber, &idesc); 157 } 158 lastino += 1; 159 if (inosused < sblock.fs_ipg || inumber == lastino) 160 continue; 161 /* 162 * If we were not able to determine in advance which inodes 163 * were in use, then reduce the size of the inoinfo structure 164 * to the size necessary to describe the inodes that we 165 * really found. 166 */ 167 if (lastino < (c * sblock.fs_ipg)) 168 inosused = 0; 169 else 170 inosused = lastino - (c * sblock.fs_ipg); 171 inostathead[c].il_numalloced = inosused; 172 if (inosused == 0) { 173 free(inostathead[c].il_stat); 174 inostathead[c].il_stat = 0; 175 continue; 176 } 177 info = calloc((unsigned)inosused, sizeof(struct inostat)); 178 if (info == NULL) 179 errx(EEXIT, "cannot alloc %u bytes for inoinfo", 180 (unsigned)(sizeof(struct inostat) * inosused)); 181 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info)); 182 free(inostathead[c].il_stat); 183 inostathead[c].il_stat = info; 184 } 185 freeinodebuf(); 186 } 187 188 static void 189 checkinode(ino_t inumber, struct inodesc *idesc) 190 { 191 union dinode *dp; 192 struct zlncnt *zlnp; 193 off_t kernmaxfilesize; 194 ufs2_daddr_t ndb; 195 mode_t mode; 196 int j, ret, offset; 197 198 dp = getnextinode(inumber); 199 mode = DIP(dp, di_mode) & IFMT; 200 if (mode == 0) { 201 if ((sblock.fs_magic == FS_UFS1_MAGIC && 202 (memcmp(dp->dp1.di_db, ufs1_zino.di_db, 203 NDADDR * sizeof(ufs1_daddr_t)) || 204 memcmp(dp->dp1.di_ib, ufs1_zino.di_ib, 205 NIADDR * sizeof(ufs1_daddr_t)) || 206 dp->dp1.di_mode || dp->dp1.di_size)) || 207 (sblock.fs_magic == FS_UFS2_MAGIC && 208 (memcmp(dp->dp2.di_db, ufs2_zino.di_db, 209 NDADDR * sizeof(ufs2_daddr_t)) || 210 memcmp(dp->dp2.di_ib, ufs2_zino.di_ib, 211 NIADDR * sizeof(ufs2_daddr_t)) || 212 dp->dp2.di_mode || dp->dp2.di_size))) { 213 pfatal("PARTIALLY ALLOCATED INODE I=%lu", 214 (u_long)inumber); 215 if (reply("CLEAR") == 1) { 216 dp = ginode(inumber); 217 clearinode(dp); 218 inodirty(); 219 } 220 } 221 inoinfo(inumber)->ino_state = USTATE; 222 return; 223 } 224 lastino = inumber; 225 /* This should match the file size limit in ffs_mountfs(). */ 226 if (sblock.fs_magic == FS_UFS1_MAGIC) 227 kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1; 228 else 229 kernmaxfilesize = sblock.fs_maxfilesize; 230 if (DIP(dp, di_size) > kernmaxfilesize || 231 DIP(dp, di_size) > sblock.fs_maxfilesize || 232 (mode == IFDIR && DIP(dp, di_size) > MAXDIRSIZE)) { 233 if (debug) 234 printf("bad size %ju:", (uintmax_t)DIP(dp, di_size)); 235 goto unknown; 236 } 237 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) { 238 dp = ginode(inumber); 239 DIP(dp, di_size) = sblock.fs_fsize; 240 DIP(dp, di_mode) = IFREG|0600; 241 inodirty(); 242 } 243 if ((mode == IFBLK || mode == IFCHR || mode == IFIFO || 244 mode == IFSOCK) && DIP(dp, di_size) != 0) { 245 if (debug) 246 printf("bad special-file size %ju:", 247 (uintmax_t)DIP(dp, di_size)); 248 goto unknown; 249 } 250 if ((mode == IFBLK || mode == IFCHR) && 251 (dev_t)DIP(dp, di_rdev) == NODEV) { 252 if (debug) 253 printf("bad special-file rdev NODEV:"); 254 goto unknown; 255 } 256 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize); 257 if (ndb < 0) { 258 if (debug) 259 printf("bad size %ju ndb %ju:", 260 (uintmax_t)DIP(dp, di_size), (uintmax_t)ndb); 261 goto unknown; 262 } 263 if (mode == IFBLK || mode == IFCHR) 264 ndb++; 265 if (mode == IFLNK) { 266 /* 267 * Fake ndb value so direct/indirect block checks below 268 * will detect any garbage after symlink string. 269 */ 270 if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) { 271 if (sblock.fs_magic == FS_UFS1_MAGIC) 272 ndb = howmany(DIP(dp, di_size), 273 sizeof(ufs1_daddr_t)); 274 else 275 ndb = howmany(DIP(dp, di_size), 276 sizeof(ufs2_daddr_t)); 277 if (ndb > NDADDR) { 278 j = ndb - NDADDR; 279 for (ndb = 1; j > 1; j--) 280 ndb *= NINDIR(&sblock); 281 ndb += NDADDR; 282 } 283 } 284 } 285 for (j = ndb; ndb < NDADDR && j < NDADDR; j++) 286 if (DIP(dp, di_db[j]) != 0) { 287 if (debug) 288 printf("bad direct addr[%d]: %ju\n", j, 289 (uintmax_t)DIP(dp, di_db[j])); 290 goto unknown; 291 } 292 for (j = 0, ndb -= NDADDR; ndb > 0; j++) 293 ndb /= NINDIR(&sblock); 294 for (; j < NIADDR; j++) 295 if (DIP(dp, di_ib[j]) != 0) { 296 if (debug) 297 printf("bad indirect addr: %ju\n", 298 (uintmax_t)DIP(dp, di_ib[j])); 299 goto unknown; 300 } 301 if (ftypeok(dp) == 0) 302 goto unknown; 303 n_files++; 304 inoinfo(inumber)->ino_linkcnt = DIP(dp, di_nlink); 305 if (DIP(dp, di_nlink) <= 0) { 306 zlnp = (struct zlncnt *)malloc(sizeof *zlnp); 307 if (zlnp == NULL) { 308 pfatal("LINK COUNT TABLE OVERFLOW"); 309 if (reply("CONTINUE") == 0) { 310 ckfini(0); 311 exit(EEXIT); 312 } 313 } else { 314 zlnp->zlncnt = inumber; 315 zlnp->next = zlnhead; 316 zlnhead = zlnp; 317 } 318 } 319 if (mode == IFDIR) { 320 if (DIP(dp, di_size) == 0) 321 inoinfo(inumber)->ino_state = DCLEAR; 322 else 323 inoinfo(inumber)->ino_state = DSTATE; 324 cacheino(dp, inumber); 325 countdirs++; 326 } else 327 inoinfo(inumber)->ino_state = FSTATE; 328 inoinfo(inumber)->ino_type = IFTODT(mode); 329 badblk = dupblk = 0; 330 idesc->id_number = inumber; 331 if (DIP(dp, di_flags) & SF_SNAPSHOT) 332 idesc->id_type = SNAP; 333 else 334 idesc->id_type = ADDR; 335 (void)ckinode(dp, idesc); 336 if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) { 337 idesc->id_type = ADDR; 338 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize); 339 for (j = 0; j < NXADDR; j++) { 340 if (--ndb == 0 && 341 (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0) 342 idesc->id_numfrags = numfrags(&sblock, 343 fragroundup(&sblock, offset)); 344 else 345 idesc->id_numfrags = sblock.fs_frag; 346 if (dp->dp2.di_extb[j] == 0) 347 continue; 348 idesc->id_blkno = dp->dp2.di_extb[j]; 349 ret = (*idesc->id_func)(idesc); 350 if (ret & STOP) 351 break; 352 } 353 } 354 if (sblock.fs_magic == FS_UFS2_MAGIC) 355 eascan(idesc, &dp->dp2); 356 idesc->id_entryno *= btodb(sblock.fs_fsize); 357 if (DIP(dp, di_blocks) != idesc->id_entryno) { 358 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)", 359 (u_long)inumber, (uintmax_t)DIP(dp, di_blocks), 360 (uintmax_t)idesc->id_entryno); 361 if (preen) 362 printf(" (CORRECTED)\n"); 363 else if (reply("CORRECT") == 0) 364 return; 365 if (bkgrdflag == 0) { 366 dp = ginode(inumber); 367 DIP(dp, di_blocks) = idesc->id_entryno; 368 inodirty(); 369 } else { 370 cmd.value = idesc->id_number; 371 cmd.size = idesc->id_entryno - DIP(dp, di_blocks); 372 if (debug) 373 printf("adjblkcnt ino %ju amount %lld\n", 374 (uintmax_t)cmd.value, (long long)cmd.size); 375 if (sysctl(adjblkcnt, MIBSIZE, 0, 0, 376 &cmd, sizeof cmd) == -1) 377 rwerror("ADJUST INODE BLOCK COUNT", cmd.value); 378 } 379 } 380 return; 381 unknown: 382 pfatal("UNKNOWN FILE TYPE I=%lu", (u_long)inumber); 383 inoinfo(inumber)->ino_state = FCLEAR; 384 if (reply("CLEAR") == 1) { 385 inoinfo(inumber)->ino_state = USTATE; 386 dp = ginode(inumber); 387 clearinode(dp); 388 inodirty(); 389 } 390 } 391 392 int 393 pass1check(struct inodesc *idesc) 394 { 395 int res = KEEPON; 396 int anyout, nfrags; 397 ufs2_daddr_t blkno = idesc->id_blkno; 398 struct dups *dlp; 399 struct dups *new; 400 401 if (idesc->id_type == SNAP) { 402 if (blkno == BLK_NOCOPY) 403 return (KEEPON); 404 if (idesc->id_number == cursnapshot) { 405 if (blkno == blkstofrags(&sblock, idesc->id_lbn)) 406 return (KEEPON); 407 if (blkno == BLK_SNAP) { 408 blkno = blkstofrags(&sblock, idesc->id_lbn); 409 idesc->id_entryno -= idesc->id_numfrags; 410 } 411 } else { 412 if (blkno == BLK_SNAP) 413 return (KEEPON); 414 } 415 } 416 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) { 417 blkerror(idesc->id_number, "BAD", blkno); 418 if (badblk++ >= MAXBAD) { 419 pwarn("EXCESSIVE BAD BLKS I=%lu", 420 (u_long)idesc->id_number); 421 if (preen) 422 printf(" (SKIPPING)\n"); 423 else if (reply("CONTINUE") == 0) { 424 ckfini(0); 425 exit(EEXIT); 426 } 427 return (STOP); 428 } 429 } 430 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { 431 if (anyout && chkrange(blkno, 1)) { 432 res = SKIP; 433 } else if (!testbmap(blkno)) { 434 n_blks++; 435 setbmap(blkno); 436 } else { 437 blkerror(idesc->id_number, "DUP", blkno); 438 if (dupblk++ >= MAXDUP) { 439 pwarn("EXCESSIVE DUP BLKS I=%lu", 440 (u_long)idesc->id_number); 441 if (preen) 442 printf(" (SKIPPING)\n"); 443 else if (reply("CONTINUE") == 0) { 444 ckfini(0); 445 exit(EEXIT); 446 } 447 return (STOP); 448 } 449 new = (struct dups *)malloc(sizeof(struct dups)); 450 if (new == NULL) { 451 pfatal("DUP TABLE OVERFLOW."); 452 if (reply("CONTINUE") == 0) { 453 ckfini(0); 454 exit(EEXIT); 455 } 456 return (STOP); 457 } 458 new->dup = blkno; 459 if (muldup == 0) { 460 duplist = muldup = new; 461 new->next = 0; 462 } else { 463 new->next = muldup->next; 464 muldup->next = new; 465 } 466 for (dlp = duplist; dlp != muldup; dlp = dlp->next) 467 if (dlp->dup == blkno) 468 break; 469 if (dlp == muldup && dlp->dup != blkno) 470 muldup = new; 471 } 472 /* 473 * count the number of blocks found in id_entryno 474 */ 475 idesc->id_entryno++; 476 } 477 return (res); 478 } 479