pass1.c revision 23675
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 457585Sbde#include <stdio.h> 461558Srgrimes#include <stdlib.h> 4723675Speter#include <err.h> 481558Srgrimes#include <string.h> 4923675Speter 501558Srgrimes#include "fsck.h" 511558Srgrimes 5223675Speterstatic ufs_daddr_t badblk; 5323675Speterstatic ufs_daddr_t dupblk; 541558Srgrimes 5523675Speterstatic void checkinode __P((ino_t inumber, struct inodesc *)); 567585Sbde 577585Sbdevoid 581558Srgrimespass1() 591558Srgrimes{ 601558Srgrimes ino_t inumber; 611558Srgrimes int c, i, cgd; 621558Srgrimes struct inodesc idesc; 631558Srgrimes 641558Srgrimes /* 651558Srgrimes * Set file system reserved blocks in used block map. 661558Srgrimes */ 671558Srgrimes for (c = 0; c < sblock.fs_ncg; c++) { 681558Srgrimes cgd = cgdmin(&sblock, c); 691558Srgrimes if (c == 0) { 701558Srgrimes i = cgbase(&sblock, c); 711558Srgrimes cgd += howmany(sblock.fs_cssize, sblock.fs_fsize); 721558Srgrimes } else 731558Srgrimes i = cgsblock(&sblock, c); 741558Srgrimes for (; i < cgd; i++) 751558Srgrimes setbmap(i); 761558Srgrimes } 771558Srgrimes /* 781558Srgrimes * Find all allocated blocks. 791558Srgrimes */ 8023675Speter memset(&idesc, 0, sizeof(struct inodesc)); 811558Srgrimes idesc.id_type = ADDR; 821558Srgrimes idesc.id_func = pass1check; 831558Srgrimes inumber = 0; 841558Srgrimes n_files = n_blks = 0; 851558Srgrimes resetinodebuf(); 861558Srgrimes for (c = 0; c < sblock.fs_ncg; c++) { 871558Srgrimes for (i = 0; i < sblock.fs_ipg; i++, inumber++) { 881558Srgrimes if (inumber < ROOTINO) 891558Srgrimes continue; 901558Srgrimes checkinode(inumber, &idesc); 911558Srgrimes } 921558Srgrimes } 931558Srgrimes freeinodebuf(); 941558Srgrimes} 951558Srgrimes 9623675Speterstatic void 971558Srgrimescheckinode(inumber, idesc) 981558Srgrimes ino_t inumber; 991558Srgrimes register struct inodesc *idesc; 1001558Srgrimes{ 1011558Srgrimes register struct dinode *dp; 1021558Srgrimes struct zlncnt *zlnp; 1031558Srgrimes int ndb, j; 1041558Srgrimes mode_t mode; 1052603Sdg char *symbuf; 1061558Srgrimes 1071558Srgrimes dp = getnextinode(inumber); 1081558Srgrimes mode = dp->di_mode & IFMT; 1091558Srgrimes if (mode == 0) { 11023675Speter if (memcmp(dp->di_db, zino.di_db, 11123675Speter NDADDR * sizeof(ufs_daddr_t)) || 11223675Speter memcmp(dp->di_ib, zino.di_ib, 11323675Speter NIADDR * sizeof(ufs_daddr_t)) || 1141558Srgrimes dp->di_mode || dp->di_size) { 1151558Srgrimes pfatal("PARTIALLY ALLOCATED INODE I=%lu", inumber); 1161558Srgrimes if (reply("CLEAR") == 1) { 1171558Srgrimes dp = ginode(inumber); 1181558Srgrimes clearinode(dp); 1191558Srgrimes inodirty(); 1201558Srgrimes } 1211558Srgrimes } 1221558Srgrimes statemap[inumber] = USTATE; 1231558Srgrimes return; 1241558Srgrimes } 1251558Srgrimes lastino = inumber; 1261558Srgrimes if (/* dp->di_size < 0 || */ 12723675Speter dp->di_size + sblock.fs_bsize - 1 < dp->di_size /* || 12823675Speter (mode == IFDIR && dp->di_size > MAXDIRSIZE) */) { 1291558Srgrimes if (debug) 1301558Srgrimes printf("bad size %qu:", dp->di_size); 1311558Srgrimes goto unknown; 1321558Srgrimes } 1331558Srgrimes if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) { 1341558Srgrimes dp = ginode(inumber); 1351558Srgrimes dp->di_size = sblock.fs_fsize; 1361558Srgrimes dp->di_mode = IFREG|0600; 1371558Srgrimes inodirty(); 1381558Srgrimes } 1391558Srgrimes ndb = howmany(dp->di_size, sblock.fs_bsize); 1401558Srgrimes if (ndb < 0) { 1411558Srgrimes if (debug) 1421558Srgrimes printf("bad size %qu ndb %d:", 1431558Srgrimes dp->di_size, ndb); 1441558Srgrimes goto unknown; 1451558Srgrimes } 1461558Srgrimes if (mode == IFBLK || mode == IFCHR) 1471558Srgrimes ndb++; 1481558Srgrimes if (mode == IFLNK) { 1491558Srgrimes if (doinglevel2 && 1501558Srgrimes dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN && 1511558Srgrimes dp->di_blocks != 0) { 1522603Sdg symbuf = alloca(secsize); 1531558Srgrimes if (bread(fsreadfd, symbuf, 1541558Srgrimes fsbtodb(&sblock, dp->di_db[0]), 1552603Sdg (long)secsize) != 0) 15623675Speter errx(EEXIT, "cannot read symlink"); 1571558Srgrimes if (debug) { 1581558Srgrimes symbuf[dp->di_size] = 0; 1597585Sbde printf("convert symlink %ld(%s) of size %ld\n", 1601558Srgrimes inumber, symbuf, (long)dp->di_size); 1611558Srgrimes } 1621558Srgrimes dp = ginode(inumber); 16323675Speter memmove(dp->di_shortlink, symbuf, (long)dp->di_size); 1641558Srgrimes dp->di_blocks = 0; 1651558Srgrimes inodirty(); 1661558Srgrimes } 1671558Srgrimes /* 1681558Srgrimes * Fake ndb value so direct/indirect block checks below 1691558Srgrimes * will detect any garbage after symlink string. 1701558Srgrimes */ 1711820Sdg if ((dp->di_size < sblock.fs_maxsymlinklen) || dp->di_blocks == 0) { 17223675Speter ndb = howmany(dp->di_size, sizeof(ufs_daddr_t)); 1731558Srgrimes if (ndb > NDADDR) { 1741558Srgrimes j = ndb - NDADDR; 1751558Srgrimes for (ndb = 1; j > 1; j--) 1761558Srgrimes ndb *= NINDIR(&sblock); 1771558Srgrimes ndb += NDADDR; 1781558Srgrimes } 1791558Srgrimes } 1801558Srgrimes } 1811558Srgrimes for (j = ndb; j < NDADDR; j++) 1821558Srgrimes if (dp->di_db[j] != 0) { 1831558Srgrimes if (debug) 1841558Srgrimes printf("bad direct addr: %ld\n", dp->di_db[j]); 1851558Srgrimes goto unknown; 1861558Srgrimes } 1871558Srgrimes for (j = 0, ndb -= NDADDR; ndb > 0; j++) 1881558Srgrimes ndb /= NINDIR(&sblock); 1891558Srgrimes for (; j < NIADDR; j++) 1901558Srgrimes if (dp->di_ib[j] != 0) { 1911558Srgrimes if (debug) 1921558Srgrimes printf("bad indirect addr: %ld\n", 1931558Srgrimes dp->di_ib[j]); 1941558Srgrimes goto unknown; 1951558Srgrimes } 1961558Srgrimes if (ftypeok(dp) == 0) 1971558Srgrimes goto unknown; 1981558Srgrimes n_files++; 1991558Srgrimes lncntp[inumber] = dp->di_nlink; 2001558Srgrimes if (dp->di_nlink <= 0) { 2011558Srgrimes zlnp = (struct zlncnt *)malloc(sizeof *zlnp); 2021558Srgrimes if (zlnp == NULL) { 2031558Srgrimes pfatal("LINK COUNT TABLE OVERFLOW"); 2041558Srgrimes if (reply("CONTINUE") == 0) 20523675Speter exit(EEXIT); 2061558Srgrimes } else { 2071558Srgrimes zlnp->zlncnt = inumber; 2081558Srgrimes zlnp->next = zlnhead; 2091558Srgrimes zlnhead = zlnp; 2101558Srgrimes } 2111558Srgrimes } 2121558Srgrimes if (mode == IFDIR) { 2131558Srgrimes if (dp->di_size == 0) 2141558Srgrimes statemap[inumber] = DCLEAR; 2151558Srgrimes else 2161558Srgrimes statemap[inumber] = DSTATE; 2171558Srgrimes cacheino(dp, inumber); 2181558Srgrimes } else 2191558Srgrimes statemap[inumber] = FSTATE; 2201558Srgrimes typemap[inumber] = IFTODT(mode); 2211558Srgrimes if (doinglevel2 && 2221558Srgrimes (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) { 2231558Srgrimes dp = ginode(inumber); 2241558Srgrimes dp->di_uid = dp->di_ouid; 2251558Srgrimes dp->di_ouid = -1; 2261558Srgrimes dp->di_gid = dp->di_ogid; 2271558Srgrimes dp->di_ogid = -1; 2281558Srgrimes inodirty(); 2291558Srgrimes } 2301558Srgrimes badblk = dupblk = 0; 2311558Srgrimes idesc->id_number = inumber; 2321558Srgrimes (void)ckinode(dp, idesc); 2331558Srgrimes idesc->id_entryno *= btodb(sblock.fs_fsize); 2341558Srgrimes if (dp->di_blocks != idesc->id_entryno) { 2351558Srgrimes pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)", 2361558Srgrimes inumber, dp->di_blocks, idesc->id_entryno); 2371558Srgrimes if (preen) 2381558Srgrimes printf(" (CORRECTED)\n"); 2391558Srgrimes else if (reply("CORRECT") == 0) 2401558Srgrimes return; 2411558Srgrimes dp = ginode(inumber); 2421558Srgrimes dp->di_blocks = idesc->id_entryno; 2431558Srgrimes inodirty(); 2441558Srgrimes } 2451558Srgrimes return; 2461558Srgrimesunknown: 2471558Srgrimes pfatal("UNKNOWN FILE TYPE I=%lu", inumber); 2481558Srgrimes statemap[inumber] = FCLEAR; 2491558Srgrimes if (reply("CLEAR") == 1) { 2501558Srgrimes statemap[inumber] = USTATE; 2511558Srgrimes dp = ginode(inumber); 2521558Srgrimes clearinode(dp); 2531558Srgrimes inodirty(); 2541558Srgrimes } 2551558Srgrimes} 2561558Srgrimes 2577585Sbdeint 2581558Srgrimespass1check(idesc) 2591558Srgrimes register struct inodesc *idesc; 2601558Srgrimes{ 2611558Srgrimes int res = KEEPON; 2621558Srgrimes int anyout, nfrags; 26323675Speter ufs_daddr_t blkno = idesc->id_blkno; 2641558Srgrimes register struct dups *dlp; 2651558Srgrimes struct dups *new; 2661558Srgrimes 2671558Srgrimes if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) { 2681558Srgrimes blkerror(idesc->id_number, "BAD", blkno); 2691558Srgrimes if (badblk++ >= MAXBAD) { 2701558Srgrimes pwarn("EXCESSIVE BAD BLKS I=%lu", 2711558Srgrimes idesc->id_number); 2721558Srgrimes if (preen) 2731558Srgrimes printf(" (SKIPPING)\n"); 2741558Srgrimes else if (reply("CONTINUE") == 0) 27523675Speter exit(EEXIT); 2761558Srgrimes return (STOP); 2771558Srgrimes } 2781558Srgrimes } 2791558Srgrimes for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) { 2801558Srgrimes if (anyout && chkrange(blkno, 1)) { 2811558Srgrimes res = SKIP; 2821558Srgrimes } else if (!testbmap(blkno)) { 2831558Srgrimes n_blks++; 2841558Srgrimes setbmap(blkno); 2851558Srgrimes } else { 2861558Srgrimes blkerror(idesc->id_number, "DUP", blkno); 2871558Srgrimes if (dupblk++ >= MAXDUP) { 2881558Srgrimes pwarn("EXCESSIVE DUP BLKS I=%lu", 2891558Srgrimes idesc->id_number); 2901558Srgrimes if (preen) 2911558Srgrimes printf(" (SKIPPING)\n"); 2921558Srgrimes else if (reply("CONTINUE") == 0) 29323675Speter exit(EEXIT); 2941558Srgrimes return (STOP); 2951558Srgrimes } 2961558Srgrimes new = (struct dups *)malloc(sizeof(struct dups)); 2971558Srgrimes if (new == NULL) { 2981558Srgrimes pfatal("DUP TABLE OVERFLOW."); 2991558Srgrimes if (reply("CONTINUE") == 0) 30023675Speter exit(EEXIT); 3011558Srgrimes return (STOP); 3021558Srgrimes } 3031558Srgrimes new->dup = blkno; 3041558Srgrimes if (muldup == 0) { 3051558Srgrimes duplist = muldup = new; 3061558Srgrimes new->next = 0; 3071558Srgrimes } else { 3081558Srgrimes new->next = muldup->next; 3091558Srgrimes muldup->next = new; 3101558Srgrimes } 3111558Srgrimes for (dlp = duplist; dlp != muldup; dlp = dlp->next) 3121558Srgrimes if (dlp->dup == blkno) 3131558Srgrimes break; 3141558Srgrimes if (dlp == muldup && dlp->dup != blkno) 3151558Srgrimes muldup = new; 3161558Srgrimes } 3171558Srgrimes /* 3181558Srgrimes * count the number of blocks found in id_entryno 3191558Srgrimes */ 3201558Srgrimes idesc->id_entryno++; 3211558Srgrimes } 3221558Srgrimes return (res); 3231558Srgrimes} 324