pass1.c revision 34266
18871Srgrimes/* 21558Srgrimes * Copyright (c) 1980, 1986, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 3523675Speterstatic const char sccsid[] = "@(#)pass1.c 8.6 (Berkeley) 4/28/95"; 361558Srgrimes#endif /* not lint */ 371558Srgrimes 381558Srgrimes#include <sys/param.h> 391558Srgrimes#include <sys/time.h> 4023675Speter 411558Srgrimes#include <ufs/ufs/dinode.h> 421558Srgrimes#include <ufs/ufs/dir.h> 431558Srgrimes#include <ufs/ffs/fs.h> 4423675Speter 4523675Speter#include <err.h> 461558Srgrimes#include <string.h> 4723675Speter 481558Srgrimes#include "fsck.h" 491558Srgrimes 5023675Speterstatic ufs_daddr_t badblk; 5123675Speterstatic ufs_daddr_t dupblk; 521558Srgrimes 5323675Speterstatic void checkinode __P((ino_t inumber, struct inodesc *)); 547585Sbde 557585Sbdevoid 561558Srgrimespass1() 571558Srgrimes{ 581558Srgrimes ino_t inumber; 591558Srgrimes int c, i, cgd; 601558Srgrimes struct inodesc idesc; 611558Srgrimes 621558Srgrimes /* 631558Srgrimes * Set file system reserved blocks in used block map. 641558Srgrimes */ 651558Srgrimes for (c = 0; c < sblock.fs_ncg; c++) { 661558Srgrimes cgd = cgdmin(&sblock, c); 671558Srgrimes if (c == 0) { 681558Srgrimes i = cgbase(&sblock, c); 691558Srgrimes cgd += howmany(sblock.fs_cssize, sblock.fs_fsize); 701558Srgrimes } else 711558Srgrimes i = cgsblock(&sblock, c); 721558Srgrimes for (; i < cgd; i++) 731558Srgrimes setbmap(i); 741558Srgrimes } 751558Srgrimes /* 761558Srgrimes * Find all allocated blocks. 771558Srgrimes */ 7823675Speter memset(&idesc, 0, sizeof(struct inodesc)); 791558Srgrimes idesc.id_type = ADDR; 801558Srgrimes idesc.id_func = pass1check; 811558Srgrimes inumber = 0; 821558Srgrimes n_files = n_blks = 0; 831558Srgrimes resetinodebuf(); 841558Srgrimes for (c = 0; c < sblock.fs_ncg; c++) { 851558Srgrimes for (i = 0; i < sblock.fs_ipg; i++, inumber++) { 861558Srgrimes if (inumber < ROOTINO) 871558Srgrimes continue; 881558Srgrimes checkinode(inumber, &idesc); 891558Srgrimes } 901558Srgrimes } 911558Srgrimes freeinodebuf(); 921558Srgrimes} 931558Srgrimes 9423675Speterstatic void 951558Srgrimescheckinode(inumber, idesc) 961558Srgrimes ino_t inumber; 971558Srgrimes register struct inodesc *idesc; 981558Srgrimes{ 991558Srgrimes register struct dinode *dp; 1001558Srgrimes struct zlncnt *zlnp; 1011558Srgrimes int ndb, j; 1021558Srgrimes mode_t mode; 1032603Sdg char *symbuf; 1041558Srgrimes 1051558Srgrimes dp = getnextinode(inumber); 1061558Srgrimes mode = dp->di_mode & IFMT; 1071558Srgrimes if (mode == 0) { 10823675Speter if (memcmp(dp->di_db, zino.di_db, 10923675Speter NDADDR * sizeof(ufs_daddr_t)) || 11023675Speter memcmp(dp->di_ib, zino.di_ib, 11123675Speter NIADDR * sizeof(ufs_daddr_t)) || 1121558Srgrimes dp->di_mode || dp->di_size) { 1131558Srgrimes pfatal("PARTIALLY ALLOCATED INODE I=%lu", inumber); 1141558Srgrimes if (reply("CLEAR") == 1) { 1151558Srgrimes dp = ginode(inumber); 1161558Srgrimes clearinode(dp); 1171558Srgrimes inodirty(); 1181558Srgrimes } 1191558Srgrimes } 1201558Srgrimes statemap[inumber] = USTATE; 1211558Srgrimes return; 1221558Srgrimes } 1231558Srgrimes lastino = inumber; 1241558Srgrimes if (/* dp->di_size < 0 || */ 12523999Speter dp->di_size + sblock.fs_bsize - 1 < dp->di_size || 12623999Speter (mode == IFDIR && dp->di_size > MAXDIRSIZE)) { 1271558Srgrimes if (debug) 1281558Srgrimes printf("bad size %qu:", dp->di_size); 1291558Srgrimes goto unknown; 1301558Srgrimes } 1311558Srgrimes if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) { 1321558Srgrimes dp = ginode(inumber); 1331558Srgrimes dp->di_size = sblock.fs_fsize; 1341558Srgrimes dp->di_mode = IFREG|0600; 1351558Srgrimes inodirty(); 1361558Srgrimes } 1371558Srgrimes ndb = howmany(dp->di_size, sblock.fs_bsize); 1381558Srgrimes if (ndb < 0) { 1391558Srgrimes if (debug) 1401558Srgrimes printf("bad size %qu ndb %d:", 1411558Srgrimes dp->di_size, ndb); 1421558Srgrimes goto unknown; 1431558Srgrimes } 1441558Srgrimes if (mode == IFBLK || mode == IFCHR) 1451558Srgrimes ndb++; 1461558Srgrimes if (mode == IFLNK) { 1471558Srgrimes if (doinglevel2 && 1481558Srgrimes dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN && 1491558Srgrimes dp->di_blocks != 0) { 1502603Sdg symbuf = alloca(secsize); 1511558Srgrimes if (bread(fsreadfd, symbuf, 1521558Srgrimes fsbtodb(&sblock, dp->di_db[0]), 1532603Sdg (long)secsize) != 0) 15423675Speter errx(EEXIT, "cannot read symlink"); 1551558Srgrimes if (debug) { 1561558Srgrimes symbuf[dp->di_size] = 0; 1577585Sbde printf("convert symlink %ld(%s) of size %ld\n", 1581558Srgrimes inumber, symbuf, (long)dp->di_size); 1591558Srgrimes } 1601558Srgrimes dp = ginode(inumber); 16123675Speter memmove(dp->di_shortlink, symbuf, (long)dp->di_size); 1621558Srgrimes dp->di_blocks = 0; 1631558Srgrimes inodirty(); 1641558Srgrimes } 1651558Srgrimes /* 1661558Srgrimes * Fake ndb value so direct/indirect block checks below 1671558Srgrimes * will detect any garbage after symlink string. 1681558Srgrimes */ 16923798Sbde if (dp->di_size < sblock.fs_maxsymlinklen || 17023798Sbde dp->di_blocks == 0) { 17123675Speter ndb = howmany(dp->di_size, sizeof(ufs_daddr_t)); 1721558Srgrimes if (ndb > NDADDR) { 1731558Srgrimes j = ndb - NDADDR; 1741558Srgrimes for (ndb = 1; j > 1; j--) 1751558Srgrimes ndb *= NINDIR(&sblock); 1761558Srgrimes ndb += NDADDR; 1771558Srgrimes } 1781558Srgrimes } 1791558Srgrimes } 1801558Srgrimes for (j = ndb; j < NDADDR; j++) 1811558Srgrimes if (dp->di_db[j] != 0) { 1821558Srgrimes if (debug) 1831558Srgrimes printf("bad direct addr: %ld\n", dp->di_db[j]); 1841558Srgrimes goto unknown; 1851558Srgrimes } 1861558Srgrimes for (j = 0, ndb -= NDADDR; ndb > 0; j++) 1871558Srgrimes ndb /= NINDIR(&sblock); 1881558Srgrimes for (; j < NIADDR; j++) 1891558Srgrimes if (dp->di_ib[j] != 0) { 1901558Srgrimes if (debug) 1911558Srgrimes printf("bad indirect addr: %ld\n", 1921558Srgrimes dp->di_ib[j]); 1931558Srgrimes goto unknown; 1941558Srgrimes } 1951558Srgrimes if (ftypeok(dp) == 0) 1961558Srgrimes goto unknown; 1971558Srgrimes n_files++; 1981558Srgrimes lncntp[inumber] = dp->di_nlink; 1991558Srgrimes if (dp->di_nlink <= 0) { 2001558Srgrimes zlnp = (struct zlncnt *)malloc(sizeof *zlnp); 2011558Srgrimes if (zlnp == NULL) { 2021558Srgrimes pfatal("LINK COUNT TABLE OVERFLOW"); 20334266Sjulian if (reply("CONTINUE") == 0) { 20434266Sjulian ckfini(0); 20523675Speter exit(EEXIT); 20634266Sjulian } 2071558Srgrimes } else { 2081558Srgrimes zlnp->zlncnt = inumber; 2091558Srgrimes zlnp->next = zlnhead; 2101558Srgrimes zlnhead = zlnp; 2111558Srgrimes } 2121558Srgrimes } 2131558Srgrimes if (mode == IFDIR) { 2141558Srgrimes if (dp->di_size == 0) 2151558Srgrimes statemap[inumber] = DCLEAR; 2161558Srgrimes else 2171558Srgrimes statemap[inumber] = DSTATE; 2181558Srgrimes cacheino(dp, inumber); 2191558Srgrimes } else 2201558Srgrimes statemap[inumber] = FSTATE; 2211558Srgrimes typemap[inumber] = IFTODT(mode); 2221558Srgrimes if (doinglevel2 && 2231558Srgrimes (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) { 2241558Srgrimes dp = ginode(inumber); 2251558Srgrimes dp->di_uid = dp->di_ouid; 2261558Srgrimes dp->di_ouid = -1; 2271558Srgrimes dp->di_gid = dp->di_ogid; 2281558Srgrimes dp->di_ogid = -1; 2291558Srgrimes inodirty(); 2301558Srgrimes } 2311558Srgrimes badblk = dupblk = 0; 2321558Srgrimes idesc->id_number = inumber; 2331558Srgrimes (void)ckinode(dp, idesc); 2341558Srgrimes idesc->id_entryno *= btodb(sblock.fs_fsize); 2351558Srgrimes if (dp->di_blocks != idesc->id_entryno) { 2361558Srgrimes pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)", 2371558Srgrimes inumber, dp->di_blocks, idesc->id_entryno); 2381558Srgrimes if (preen) 2391558Srgrimes printf(" (CORRECTED)\n"); 2401558Srgrimes else if (reply("CORRECT") == 0) 2411558Srgrimes return; 2421558Srgrimes dp = ginode(inumber); 2431558Srgrimes dp->di_blocks = idesc->id_entryno; 2441558Srgrimes inodirty(); 2451558Srgrimes } 2461558Srgrimes return; 2471558Srgrimesunknown: 2481558Srgrimes pfatal("UNKNOWN FILE TYPE I=%lu", inumber); 2491558Srgrimes statemap[inumber] = FCLEAR; 2501558Srgrimes if (reply("CLEAR") == 1) { 2511558Srgrimes statemap[inumber] = USTATE; 2521558Srgrimes dp = ginode(inumber); 2531558Srgrimes clearinode(dp); 2541558Srgrimes inodirty(); 2551558Srgrimes } 2561558Srgrimes} 2571558Srgrimes 2587585Sbdeint 2591558Srgrimespass1check(idesc) 2601558Srgrimes register struct inodesc *idesc; 2611558Srgrimes{ 2621558Srgrimes int res = KEEPON; 2631558Srgrimes int anyout, nfrags; 26423675Speter ufs_daddr_t blkno = idesc->id_blkno; 2651558Srgrimes register struct dups *dlp; 2661558Srgrimes struct dups *new; 2671558Srgrimes 2681558Srgrimes if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) { 2691558Srgrimes blkerror(idesc->id_number, "BAD", blkno); 2701558Srgrimes if (badblk++ >= MAXBAD) { 2711558Srgrimes pwarn("EXCESSIVE BAD BLKS I=%lu", 2721558Srgrimes idesc->id_number); 2731558Srgrimes if (preen) 2741558Srgrimes printf(" (SKIPPING)\n"); 27534266Sjulian else if (reply("CONTINUE") == 0) { 27634266Sjulian ckfini(0); 27723675Speter exit(EEXIT); 27834266Sjulian } 2791558Srgrimes return (STOP); 2801558Srgrimes } 2811558Srgrimes } 2821558Srgrimes for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { 2831558Srgrimes if (anyout && chkrange(blkno, 1)) { 2841558Srgrimes res = SKIP; 2851558Srgrimes } else if (!testbmap(blkno)) { 2861558Srgrimes n_blks++; 2871558Srgrimes setbmap(blkno); 2881558Srgrimes } else { 2891558Srgrimes blkerror(idesc->id_number, "DUP", blkno); 2901558Srgrimes if (dupblk++ >= MAXDUP) { 2911558Srgrimes pwarn("EXCESSIVE DUP BLKS I=%lu", 2921558Srgrimes idesc->id_number); 2931558Srgrimes if (preen) 2941558Srgrimes printf(" (SKIPPING)\n"); 29534266Sjulian else if (reply("CONTINUE") == 0) { 29634266Sjulian ckfini(0); 29723675Speter exit(EEXIT); 29834266Sjulian } 2991558Srgrimes return (STOP); 3001558Srgrimes } 3011558Srgrimes new = (struct dups *)malloc(sizeof(struct dups)); 3021558Srgrimes if (new == NULL) { 3031558Srgrimes pfatal("DUP TABLE OVERFLOW."); 30434266Sjulian if (reply("CONTINUE") == 0) { 30534266Sjulian ckfini(0); 30623675Speter exit(EEXIT); 30734266Sjulian } 3081558Srgrimes return (STOP); 3091558Srgrimes } 3101558Srgrimes new->dup = blkno; 3111558Srgrimes if (muldup == 0) { 3121558Srgrimes duplist = muldup = new; 3131558Srgrimes new->next = 0; 3141558Srgrimes } else { 3151558Srgrimes new->next = muldup->next; 3161558Srgrimes muldup->next = new; 3171558Srgrimes } 3181558Srgrimes for (dlp = duplist; dlp != muldup; dlp = dlp->next) 3191558Srgrimes if (dlp->dup == blkno) 3201558Srgrimes break; 3211558Srgrimes if (dlp == muldup && dlp->dup != blkno) 3221558Srgrimes muldup = new; 3231558Srgrimes } 3241558Srgrimes /* 3251558Srgrimes * count the number of blocks found in id_entryno 3261558Srgrimes */ 3271558Srgrimes idesc->id_entryno++; 3281558Srgrimes } 3291558Srgrimes return (res); 3301558Srgrimes} 331