pass1.c revision 71884
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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35#if 0 36static const char sccsid[] = "@(#)pass1.c 8.6 (Berkeley) 4/28/95"; 37#endif 38static const char rcsid[] = 39 "$FreeBSD: head/sbin/fsck_ffs/pass1.c 71884 2001-01-31 15:16:56Z iedowse $"; 40#endif /* not lint */ 41 42#include <sys/param.h> 43#include <sys/stat.h> 44 45#include <ufs/ufs/dinode.h> 46#include <ufs/ufs/dir.h> 47#include <ufs/ffs/fs.h> 48 49#include <err.h> 50#include <string.h> 51 52#include "fsck.h" 53 54static ufs_daddr_t badblk; 55static ufs_daddr_t dupblk; 56static ino_t lastino; /* last inode in use */ 57 58static void checkinode __P((ino_t inumber, struct inodesc *)); 59 60void 61pass1() 62{ 63 u_int8_t *cp; 64 ino_t inumber; 65 int c, i, cgd, inosused; 66 struct inostat *info; 67 struct inodesc idesc; 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 inosused = sblock.fs_ipg; 96 if (got_siginfo) { 97 printf("%s: phase 1: cyl group %d of %d (%d%%)\n", 98 cdevname, c, sblock.fs_ncg, 99 c * 100 / sblock.fs_ncg); 100 got_siginfo = 0; 101 } 102 /* 103 * If we are using soft updates, then we can trust the 104 * cylinder group inode allocation maps to tell us which 105 * inodes are allocated. We will scan the used inode map 106 * to find the inodes that are really in use, and then 107 * read only those inodes in from disk. 108 */ 109 if (preen && usedsoftdep) { 110 getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize); 111 if (!cg_chkmagic(&cgrp)) 112 pfatal("CG %d: BAD MAGIC NUMBER\n", c); 113 cp = &cg_inosused(&cgrp)[(sblock.fs_ipg - 1) / NBBY]; 114 for ( ; inosused > 0; inosused -= NBBY, cp--) { 115 if (*cp == 0) 116 continue; 117 for (i = 1 << (NBBY - 1); i > 0; i >>= 1) { 118 if (*cp & i) 119 break; 120 inosused--; 121 } 122 break; 123 } 124 if (inosused < 0) 125 inosused = 0; 126 } 127 /* 128 * Allocate inoinfo structures for the allocated inodes. 129 */ 130 inostathead[c].il_numalloced = inosused; 131 if (inosused == 0) { 132 inostathead[c].il_stat = 0; 133 continue; 134 } 135 info = calloc((unsigned)inosused, sizeof(struct inostat)); 136 if (info == NULL) 137 pfatal("cannot alloc %u bytes for inoinfo\n", 138 (unsigned)(sizeof(struct inostat) * inosused)); 139 inostathead[c].il_stat = info; 140 /* 141 * Scan the allocated inodes. 142 */ 143 for (i = 0; i < inosused; i++, inumber++) { 144 if (inumber < ROOTINO) { 145 (void)getnextinode(inumber); 146 continue; 147 } 148 checkinode(inumber, &idesc); 149 } 150 lastino += 1; 151 if (inosused < sblock.fs_ipg || inumber == lastino) 152 continue; 153 /* 154 * If we were not able to determine in advance which inodes 155 * were in use, then reduce the size of the inoinfo structure 156 * to the size necessary to describe the inodes that we 157 * really found. 158 */ 159 inosused = lastino - (c * sblock.fs_ipg); 160 if (inosused < 0) 161 inosused = 0; 162 inostathead[c].il_numalloced = inosused; 163 if (inosused == 0) { 164 free(inostathead[c].il_stat); 165 inostathead[c].il_stat = 0; 166 continue; 167 } 168 info = calloc((unsigned)inosused, sizeof(struct inostat)); 169 if (info == NULL) 170 pfatal("cannot alloc %u bytes for inoinfo\n", 171 (unsigned)(sizeof(struct inostat) * inosused)); 172 memmove(info, inostathead[c].il_stat, inosused * sizeof(*info)); 173 free(inostathead[c].il_stat); 174 inostathead[c].il_stat = info; 175 } 176 freeinodebuf(); 177} 178 179static void 180checkinode(inumber, idesc) 181 ino_t inumber; 182 register struct inodesc *idesc; 183{ 184 register struct dinode *dp; 185 struct zlncnt *zlnp; 186 u_int64_t kernmaxfilesize; 187 ufs_daddr_t ndb, j; 188 mode_t mode; 189 char *symbuf; 190 191 dp = getnextinode(inumber); 192 mode = dp->di_mode & IFMT; 193 if (mode == 0) { 194 if (memcmp(dp->di_db, zino.di_db, 195 NDADDR * sizeof(ufs_daddr_t)) || 196 memcmp(dp->di_ib, zino.di_ib, 197 NIADDR * sizeof(ufs_daddr_t)) || 198 dp->di_mode || dp->di_size) { 199 pfatal("PARTIALLY ALLOCATED INODE I=%lu", inumber); 200 if (reply("CLEAR") == 1) { 201 dp = ginode(inumber); 202 clearinode(dp); 203 inodirty(); 204 } 205 } 206 inoinfo(inumber)->ino_state = USTATE; 207 return; 208 } 209 lastino = inumber; 210 /* This should match the file size limit in ffs_mountfs(). */ 211 kernmaxfilesize = (u_int64_t)0x40000000 * sblock.fs_bsize - 1; 212 if (dp->di_size > kernmaxfilesize || 213 dp->di_size > sblock.fs_maxfilesize || 214 (mode == IFDIR && dp->di_size > MAXDIRSIZE)) { 215 if (debug) 216 printf("bad size %qu:", dp->di_size); 217 goto unknown; 218 } 219 if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) { 220 dp = ginode(inumber); 221 dp->di_size = sblock.fs_fsize; 222 dp->di_mode = IFREG|0600; 223 inodirty(); 224 } 225 if ((mode == IFBLK || mode == IFCHR || mode == IFIFO || 226 mode == IFSOCK) && dp->di_size != 0) { 227 if (debug) 228 printf("bad special-file size %qu:", dp->di_size); 229 goto unknown; 230 } 231 ndb = howmany(dp->di_size, sblock.fs_bsize); 232 if (ndb < 0) { 233 if (debug) 234 printf("bad size %qu ndb %d:", 235 dp->di_size, ndb); 236 goto unknown; 237 } 238 if (mode == IFBLK || mode == IFCHR) 239 ndb++; 240 if (mode == IFLNK) { 241 if (doinglevel2 && 242 dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN && 243 dp->di_blocks != 0) { 244 symbuf = alloca(secsize); 245 if (bread(fsreadfd, symbuf, 246 fsbtodb(&sblock, dp->di_db[0]), 247 (long)secsize) != 0) 248 errx(EEXIT, "cannot read symlink"); 249 if (debug) { 250 symbuf[dp->di_size] = 0; 251 printf("convert symlink %lu(%s) of size %ld\n", 252 (u_long)inumber, symbuf, (long)dp->di_size); 253 } 254 dp = ginode(inumber); 255 memmove(dp->di_shortlink, symbuf, (long)dp->di_size); 256 dp->di_blocks = 0; 257 inodirty(); 258 } 259 /* 260 * Fake ndb value so direct/indirect block checks below 261 * will detect any garbage after symlink string. 262 */ 263 if (dp->di_size < sblock.fs_maxsymlinklen) { 264 ndb = howmany(dp->di_size, sizeof(ufs_daddr_t)); 265 if (ndb > NDADDR) { 266 j = ndb - NDADDR; 267 for (ndb = 1; j > 1; j--) 268 ndb *= NINDIR(&sblock); 269 ndb += NDADDR; 270 } 271 } 272 } 273 for (j = ndb; j < NDADDR; j++) 274 if (dp->di_db[j] != 0) { 275 if (debug) 276 printf("bad direct addr: %ld\n", 277 (long)dp->di_db[j]); 278 goto unknown; 279 } 280 for (j = 0, ndb -= NDADDR; ndb > 0; j++) 281 ndb /= NINDIR(&sblock); 282 for (; j < NIADDR; j++) 283 if (dp->di_ib[j] != 0) { 284 if (debug) 285 printf("bad indirect addr: %ld\n", 286 (long)dp->di_ib[j]); 287 goto unknown; 288 } 289 if (ftypeok(dp) == 0) 290 goto unknown; 291 n_files++; 292 inoinfo(inumber)->ino_linkcnt = dp->di_nlink; 293 if (dp->di_nlink <= 0) { 294 zlnp = (struct zlncnt *)malloc(sizeof *zlnp); 295 if (zlnp == NULL) { 296 pfatal("LINK COUNT TABLE OVERFLOW"); 297 if (reply("CONTINUE") == 0) { 298 ckfini(0); 299 exit(EEXIT); 300 } 301 } else { 302 zlnp->zlncnt = inumber; 303 zlnp->next = zlnhead; 304 zlnhead = zlnp; 305 } 306 } 307 if (mode == IFDIR) { 308 if (dp->di_size == 0) 309 inoinfo(inumber)->ino_state = DCLEAR; 310 else 311 inoinfo(inumber)->ino_state = DSTATE; 312 cacheino(dp, inumber); 313 countdirs++; 314 } else 315 inoinfo(inumber)->ino_state = FSTATE; 316 inoinfo(inumber)->ino_type = IFTODT(mode); 317 if (doinglevel2 && 318 (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) { 319 dp = ginode(inumber); 320 dp->di_uid = dp->di_ouid; 321 dp->di_ouid = -1; 322 dp->di_gid = dp->di_ogid; 323 dp->di_ogid = -1; 324 inodirty(); 325 } 326 badblk = dupblk = 0; 327 idesc->id_number = inumber; 328 if (dp->di_flags & SF_SNAPSHOT) 329 idesc->id_type = SNAP; 330 else 331 idesc->id_type = ADDR; 332 (void)ckinode(dp, idesc); 333 idesc->id_entryno *= btodb(sblock.fs_fsize); 334 if (dp->di_blocks != idesc->id_entryno) { 335 pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)", 336 inumber, dp->di_blocks, idesc->id_entryno); 337 if (preen) 338 printf(" (CORRECTED)\n"); 339 else if (reply("CORRECT") == 0) 340 return; 341 dp = ginode(inumber); 342 dp->di_blocks = idesc->id_entryno; 343 inodirty(); 344 } 345 return; 346unknown: 347 pfatal("UNKNOWN FILE TYPE I=%lu", inumber); 348 inoinfo(inumber)->ino_state = FCLEAR; 349 if (reply("CLEAR") == 1) { 350 inoinfo(inumber)->ino_state = USTATE; 351 dp = ginode(inumber); 352 clearinode(dp); 353 inodirty(); 354 } 355} 356 357int 358pass1check(idesc) 359 register struct inodesc *idesc; 360{ 361 int res = KEEPON; 362 int anyout, nfrags; 363 ufs_daddr_t blkno = idesc->id_blkno; 364 register struct dups *dlp; 365 struct dups *new; 366 367 if (idesc->id_type == SNAP) { 368 if (blkno == BLK_NOCOPY) 369 return (KEEPON); 370 if (idesc->id_number == cursnapshot) { 371 if (blkno == blkstofrags(&sblock, idesc->id_lbn)) 372 return (KEEPON); 373 if (blkno == BLK_SNAP) { 374 blkno = blkstofrags(&sblock, idesc->id_lbn); 375 idesc->id_entryno -= idesc->id_numfrags; 376 } 377 } else { 378 if (blkno == BLK_SNAP) 379 return (KEEPON); 380 } 381 } 382 if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) { 383 blkerror(idesc->id_number, "BAD", blkno); 384 if (badblk++ >= MAXBAD) { 385 pwarn("EXCESSIVE BAD BLKS I=%lu", 386 idesc->id_number); 387 if (preen) 388 printf(" (SKIPPING)\n"); 389 else if (reply("CONTINUE") == 0) { 390 ckfini(0); 391 exit(EEXIT); 392 } 393 return (STOP); 394 } 395 } 396 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { 397 if (anyout && chkrange(blkno, 1)) { 398 res = SKIP; 399 } else if (!testbmap(blkno)) { 400 n_blks++; 401 setbmap(blkno); 402 } else { 403 blkerror(idesc->id_number, "DUP", blkno); 404 if (dupblk++ >= MAXDUP) { 405 pwarn("EXCESSIVE DUP BLKS I=%lu", 406 idesc->id_number); 407 if (preen) 408 printf(" (SKIPPING)\n"); 409 else if (reply("CONTINUE") == 0) { 410 ckfini(0); 411 exit(EEXIT); 412 } 413 return (STOP); 414 } 415 new = (struct dups *)malloc(sizeof(struct dups)); 416 if (new == NULL) { 417 pfatal("DUP TABLE OVERFLOW."); 418 if (reply("CONTINUE") == 0) { 419 ckfini(0); 420 exit(EEXIT); 421 } 422 return (STOP); 423 } 424 new->dup = blkno; 425 if (muldup == 0) { 426 duplist = muldup = new; 427 new->next = 0; 428 } else { 429 new->next = muldup->next; 430 muldup->next = new; 431 } 432 for (dlp = duplist; dlp != muldup; dlp = dlp->next) 433 if (dlp->dup == blkno) 434 break; 435 if (dlp == muldup && dlp->dup != blkno) 436 muldup = new; 437 } 438 /* 439 * count the number of blocks found in id_entryno 440 */ 441 idesc->id_entryno++; 442 } 443 return (res); 444} 445