pass1.c revision 63003
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
3541477Sjulian#if 0
3623675Speterstatic const char sccsid[] = "@(#)pass1.c	8.6 (Berkeley) 4/28/95";
3741477Sjulian#endif
3841477Sjulianstatic const char rcsid[] =
3950476Speter  "$FreeBSD: head/sbin/fsck_ffs/pass1.c 63003 2000-07-12 06:19:22Z mckusick $";
401558Srgrimes#endif /* not lint */
411558Srgrimes
421558Srgrimes#include <sys/param.h>
4362668Smckusick#include <sys/stat.h>
4423675Speter
451558Srgrimes#include <ufs/ufs/dinode.h>
461558Srgrimes#include <ufs/ufs/dir.h>
471558Srgrimes#include <ufs/ffs/fs.h>
4823675Speter
4923675Speter#include <err.h>
501558Srgrimes#include <string.h>
5123675Speter
521558Srgrimes#include "fsck.h"
531558Srgrimes
5423675Speterstatic ufs_daddr_t badblk;
5523675Speterstatic ufs_daddr_t dupblk;
5641474Sjulianstatic ino_t lastino;		/* last inode in use */
571558Srgrimes
5823675Speterstatic void checkinode __P((ino_t inumber, struct inodesc *));
597585Sbde
607585Sbdevoid
611558Srgrimespass1()
621558Srgrimes{
6341474Sjulian	u_int8_t *cp;
641558Srgrimes	ino_t inumber;
6541474Sjulian	int c, i, cgd, inosused;
6641474Sjulian	struct inostat *info;
671558Srgrimes	struct inodesc idesc;
681558Srgrimes
691558Srgrimes	/*
701558Srgrimes	 * Set file system reserved blocks in used block map.
711558Srgrimes	 */
721558Srgrimes	for (c = 0; c < sblock.fs_ncg; c++) {
731558Srgrimes		cgd = cgdmin(&sblock, c);
741558Srgrimes		if (c == 0) {
751558Srgrimes			i = cgbase(&sblock, c);
761558Srgrimes			cgd += howmany(sblock.fs_cssize, sblock.fs_fsize);
771558Srgrimes		} else
781558Srgrimes			i = cgsblock(&sblock, c);
791558Srgrimes		for (; i < cgd; i++)
801558Srgrimes			setbmap(i);
811558Srgrimes	}
821558Srgrimes	/*
831558Srgrimes	 * Find all allocated blocks.
841558Srgrimes	 */
8523675Speter	memset(&idesc, 0, sizeof(struct inodesc));
861558Srgrimes	idesc.id_func = pass1check;
871558Srgrimes	n_files = n_blks = 0;
881558Srgrimes	for (c = 0; c < sblock.fs_ncg; c++) {
8941474Sjulian		inumber = c * sblock.fs_ipg;
9041474Sjulian		setinodebuf(inumber);
9141474Sjulian		inosused = sblock.fs_ipg;
9241474Sjulian		/*
9341474Sjulian		 * If we are using soft updates, then we can trust the
9441474Sjulian		 * cylinder group inode allocation maps to tell us which
9541474Sjulian		 * inodes are allocated. We will scan the used inode map
9641474Sjulian		 * to find the inodes that are really in use, and then
9741474Sjulian		 * read only those inodes in from disk.
9841474Sjulian		 */
9941474Sjulian		if (preen && usedsoftdep) {
10041474Sjulian			getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize);
10141474Sjulian			if (!cg_chkmagic(&cgrp))
10241474Sjulian				pfatal("CG %d: BAD MAGIC NUMBER\n", c);
10341474Sjulian			cp = &cg_inosused(&cgrp)[(sblock.fs_ipg - 1) / NBBY];
10441474Sjulian			for ( ; inosused > 0; inosused -= NBBY, cp--) {
10541474Sjulian				if (*cp == 0)
10641474Sjulian					continue;
10741474Sjulian				for (i = 1 << (NBBY - 1); i > 0; i >>= 1) {
10841474Sjulian					if (*cp & i)
10941474Sjulian						break;
11041474Sjulian					inosused--;
11141474Sjulian				}
11241474Sjulian				break;
11341474Sjulian			}
11441474Sjulian			if (inosused < 0)
11541474Sjulian				inosused = 0;
11641474Sjulian		}
11741474Sjulian		/*
11841474Sjulian		 * Allocate inoinfo structures for the allocated inodes.
11941474Sjulian		 */
12041474Sjulian		inostathead[c].il_numalloced = inosused;
12141474Sjulian		if (inosused == 0) {
12241474Sjulian			inostathead[c].il_stat = 0;
12341474Sjulian			continue;
12441474Sjulian		}
12541474Sjulian		info = calloc((unsigned)inosused, sizeof(struct inostat));
12641474Sjulian		if (info == NULL)
12741474Sjulian			pfatal("cannot alloc %u bytes for inoinfo\n",
12841474Sjulian			    (unsigned)(sizeof(struct inostat) * inosused));
12941474Sjulian		inostathead[c].il_stat = info;
13041474Sjulian		/*
13141474Sjulian		 * Scan the allocated inodes.
13241474Sjulian		 */
13341474Sjulian		for (i = 0; i < inosused; i++, inumber++) {
13441474Sjulian			if (inumber < ROOTINO) {
13541474Sjulian				(void)getnextinode(inumber);
1361558Srgrimes				continue;
13741474Sjulian			}
1381558Srgrimes			checkinode(inumber, &idesc);
1391558Srgrimes		}
14041474Sjulian		lastino += 1;
14141474Sjulian		if (inosused < sblock.fs_ipg || inumber == lastino)
14241474Sjulian			continue;
14341474Sjulian		/*
14441474Sjulian		 * If we were not able to determine in advance which inodes
14541474Sjulian		 * were in use, then reduce the size of the inoinfo structure
14641474Sjulian		 * to the size necessary to describe the inodes that we
14741474Sjulian		 * really found.
14841474Sjulian		 */
14941474Sjulian		inosused = lastino - (c * sblock.fs_ipg);
15041474Sjulian		if (inosused < 0)
15141474Sjulian			inosused = 0;
15241474Sjulian		inostathead[c].il_numalloced = inosused;
15341474Sjulian		if (inosused == 0) {
15441474Sjulian			free(inostathead[c].il_stat);
15541474Sjulian			inostathead[c].il_stat = 0;
15641474Sjulian			continue;
15741474Sjulian		}
15841474Sjulian		info = calloc((unsigned)inosused, sizeof(struct inostat));
15941474Sjulian		if (info == NULL)
16041474Sjulian			pfatal("cannot alloc %u bytes for inoinfo\n",
16141474Sjulian			    (unsigned)(sizeof(struct inostat) * inosused));
16241474Sjulian		memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
16341474Sjulian		free(inostathead[c].il_stat);
16441474Sjulian		inostathead[c].il_stat = info;
1651558Srgrimes	}
1661558Srgrimes	freeinodebuf();
1671558Srgrimes}
1681558Srgrimes
16923675Speterstatic void
1701558Srgrimescheckinode(inumber, idesc)
1711558Srgrimes	ino_t inumber;
1721558Srgrimes	register struct inodesc *idesc;
1731558Srgrimes{
1741558Srgrimes	register struct dinode *dp;
1751558Srgrimes	struct zlncnt *zlnp;
1761558Srgrimes	int ndb, j;
1771558Srgrimes	mode_t mode;
1782603Sdg	char *symbuf;
1791558Srgrimes
1801558Srgrimes	dp = getnextinode(inumber);
1811558Srgrimes	mode = dp->di_mode & IFMT;
1821558Srgrimes	if (mode == 0) {
18323675Speter		if (memcmp(dp->di_db, zino.di_db,
18423675Speter			NDADDR * sizeof(ufs_daddr_t)) ||
18523675Speter		    memcmp(dp->di_ib, zino.di_ib,
18623675Speter			NIADDR * sizeof(ufs_daddr_t)) ||
1871558Srgrimes		    dp->di_mode || dp->di_size) {
1881558Srgrimes			pfatal("PARTIALLY ALLOCATED INODE I=%lu", inumber);
1891558Srgrimes			if (reply("CLEAR") == 1) {
1901558Srgrimes				dp = ginode(inumber);
1911558Srgrimes				clearinode(dp);
1921558Srgrimes				inodirty();
1931558Srgrimes			}
1941558Srgrimes		}
19541474Sjulian		inoinfo(inumber)->ino_state = USTATE;
1961558Srgrimes		return;
1971558Srgrimes	}
1981558Srgrimes	lastino = inumber;
1991558Srgrimes	if (/* dp->di_size < 0 || */
20023999Speter	    dp->di_size + sblock.fs_bsize - 1 < dp->di_size ||
20123999Speter	    (mode == IFDIR && dp->di_size > MAXDIRSIZE)) {
2021558Srgrimes		if (debug)
2031558Srgrimes			printf("bad size %qu:", dp->di_size);
2041558Srgrimes		goto unknown;
2051558Srgrimes	}
2061558Srgrimes	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
2071558Srgrimes		dp = ginode(inumber);
2081558Srgrimes		dp->di_size = sblock.fs_fsize;
2091558Srgrimes		dp->di_mode = IFREG|0600;
2101558Srgrimes		inodirty();
2111558Srgrimes	}
21263003Smckusick	if ((mode == IFBLK || mode == IFCHR || mode == IFIFO ||
21363003Smckusick	     mode == IFSOCK) && dp->di_size != 0) {
21463003Smckusick		if (debug)
21563003Smckusick			printf("bad special-file size %qu:", dp->di_size);
21663003Smckusick		goto unknown;
21763003Smckusick	}
2181558Srgrimes	ndb = howmany(dp->di_size, sblock.fs_bsize);
2191558Srgrimes	if (ndb < 0) {
2201558Srgrimes		if (debug)
2211558Srgrimes			printf("bad size %qu ndb %d:",
2221558Srgrimes				dp->di_size, ndb);
2231558Srgrimes		goto unknown;
2241558Srgrimes	}
2251558Srgrimes	if (mode == IFBLK || mode == IFCHR)
2261558Srgrimes		ndb++;
2271558Srgrimes	if (mode == IFLNK) {
2281558Srgrimes		if (doinglevel2 &&
2291558Srgrimes		    dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
2301558Srgrimes		    dp->di_blocks != 0) {
2312603Sdg			symbuf = alloca(secsize);
2321558Srgrimes			if (bread(fsreadfd, symbuf,
2331558Srgrimes			    fsbtodb(&sblock, dp->di_db[0]),
2342603Sdg			    (long)secsize) != 0)
23523675Speter				errx(EEXIT, "cannot read symlink");
2361558Srgrimes			if (debug) {
2371558Srgrimes				symbuf[dp->di_size] = 0;
23837236Sbde				printf("convert symlink %lu(%s) of size %ld\n",
23941474Sjulian				    (u_long)inumber, symbuf, (long)dp->di_size);
2401558Srgrimes			}
2411558Srgrimes			dp = ginode(inumber);
24223675Speter			memmove(dp->di_shortlink, symbuf, (long)dp->di_size);
2431558Srgrimes			dp->di_blocks = 0;
2441558Srgrimes			inodirty();
2451558Srgrimes		}
2461558Srgrimes		/*
2471558Srgrimes		 * Fake ndb value so direct/indirect block checks below
2481558Srgrimes		 * will detect any garbage after symlink string.
2491558Srgrimes		 */
25041474Sjulian		if (dp->di_size < sblock.fs_maxsymlinklen) {
25123675Speter			ndb = howmany(dp->di_size, sizeof(ufs_daddr_t));
2521558Srgrimes			if (ndb > NDADDR) {
2531558Srgrimes				j = ndb - NDADDR;
2541558Srgrimes				for (ndb = 1; j > 1; j--)
2551558Srgrimes					ndb *= NINDIR(&sblock);
2561558Srgrimes				ndb += NDADDR;
2571558Srgrimes			}
2581558Srgrimes		}
2591558Srgrimes	}
2601558Srgrimes	for (j = ndb; j < NDADDR; j++)
2611558Srgrimes		if (dp->di_db[j] != 0) {
2621558Srgrimes			if (debug)
26337236Sbde				printf("bad direct addr: %ld\n",
26437236Sbde				    (long)dp->di_db[j]);
2651558Srgrimes			goto unknown;
2661558Srgrimes		}
2671558Srgrimes	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
2681558Srgrimes		ndb /= NINDIR(&sblock);
2691558Srgrimes	for (; j < NIADDR; j++)
2701558Srgrimes		if (dp->di_ib[j] != 0) {
2711558Srgrimes			if (debug)
2721558Srgrimes				printf("bad indirect addr: %ld\n",
27337236Sbde				    (long)dp->di_ib[j]);
2741558Srgrimes			goto unknown;
2751558Srgrimes		}
2761558Srgrimes	if (ftypeok(dp) == 0)
2771558Srgrimes		goto unknown;
2781558Srgrimes	n_files++;
27941474Sjulian	inoinfo(inumber)->ino_linkcnt = dp->di_nlink;
2801558Srgrimes	if (dp->di_nlink <= 0) {
2811558Srgrimes		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
2821558Srgrimes		if (zlnp == NULL) {
2831558Srgrimes			pfatal("LINK COUNT TABLE OVERFLOW");
28434266Sjulian			if (reply("CONTINUE") == 0) {
28534266Sjulian				ckfini(0);
28623675Speter				exit(EEXIT);
28734266Sjulian			}
2881558Srgrimes		} else {
2891558Srgrimes			zlnp->zlncnt = inumber;
2901558Srgrimes			zlnp->next = zlnhead;
2911558Srgrimes			zlnhead = zlnp;
2921558Srgrimes		}
2931558Srgrimes	}
2941558Srgrimes	if (mode == IFDIR) {
2951558Srgrimes		if (dp->di_size == 0)
29641474Sjulian			inoinfo(inumber)->ino_state = DCLEAR;
2971558Srgrimes		else
29841474Sjulian			inoinfo(inumber)->ino_state = DSTATE;
2991558Srgrimes		cacheino(dp, inumber);
30041474Sjulian		countdirs++;
3011558Srgrimes	} else
30241474Sjulian		inoinfo(inumber)->ino_state = FSTATE;
30341474Sjulian	inoinfo(inumber)->ino_type = IFTODT(mode);
3041558Srgrimes	if (doinglevel2 &&
3051558Srgrimes	    (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
3061558Srgrimes		dp = ginode(inumber);
3071558Srgrimes		dp->di_uid = dp->di_ouid;
3081558Srgrimes		dp->di_ouid = -1;
3091558Srgrimes		dp->di_gid = dp->di_ogid;
3101558Srgrimes		dp->di_ogid = -1;
3111558Srgrimes		inodirty();
3121558Srgrimes	}
3131558Srgrimes	badblk = dupblk = 0;
3141558Srgrimes	idesc->id_number = inumber;
31562668Smckusick	if (dp->di_flags & SF_SNAPSHOT)
31662668Smckusick		idesc->id_type = SNAP;
31762668Smckusick	else
31862668Smckusick		idesc->id_type = ADDR;
3191558Srgrimes	(void)ckinode(dp, idesc);
3201558Srgrimes	idesc->id_entryno *= btodb(sblock.fs_fsize);
3211558Srgrimes	if (dp->di_blocks != idesc->id_entryno) {
3221558Srgrimes		pwarn("INCORRECT BLOCK COUNT I=%lu (%ld should be %ld)",
3231558Srgrimes		    inumber, dp->di_blocks, idesc->id_entryno);
3241558Srgrimes		if (preen)
3251558Srgrimes			printf(" (CORRECTED)\n");
3261558Srgrimes		else if (reply("CORRECT") == 0)
3271558Srgrimes			return;
3281558Srgrimes		dp = ginode(inumber);
3291558Srgrimes		dp->di_blocks = idesc->id_entryno;
3301558Srgrimes		inodirty();
3311558Srgrimes	}
3321558Srgrimes	return;
3331558Srgrimesunknown:
3341558Srgrimes	pfatal("UNKNOWN FILE TYPE I=%lu", inumber);
33541474Sjulian	inoinfo(inumber)->ino_state = FCLEAR;
3361558Srgrimes	if (reply("CLEAR") == 1) {
33741474Sjulian		inoinfo(inumber)->ino_state = USTATE;
3381558Srgrimes		dp = ginode(inumber);
3391558Srgrimes		clearinode(dp);
3401558Srgrimes		inodirty();
3411558Srgrimes	}
3421558Srgrimes}
3431558Srgrimes
3447585Sbdeint
3451558Srgrimespass1check(idesc)
3461558Srgrimes	register struct inodesc *idesc;
3471558Srgrimes{
3481558Srgrimes	int res = KEEPON;
3491558Srgrimes	int anyout, nfrags;
35023675Speter	ufs_daddr_t blkno = idesc->id_blkno;
3511558Srgrimes	register struct dups *dlp;
3521558Srgrimes	struct dups *new;
3531558Srgrimes
35462668Smckusick	if (idesc->id_type == SNAP) {
35562668Smckusick		if (blkno == BLK_NOCOPY)
35662668Smckusick			return (KEEPON);
35762668Smckusick		if (idesc->id_number == cursnapshot) {
35862668Smckusick			if (blkno == blkstofrags(&sblock, idesc->id_lbn))
35962668Smckusick				return (KEEPON);
36062668Smckusick			if (blkno == BLK_SNAP) {
36162668Smckusick				blkno = blkstofrags(&sblock, idesc->id_lbn);
36262668Smckusick				idesc->id_entryno -= idesc->id_numfrags;
36362668Smckusick			}
36462668Smckusick		} else {
36562668Smckusick			if (blkno == BLK_SNAP)
36662668Smckusick				return (KEEPON);
36762668Smckusick		}
36862668Smckusick	}
3691558Srgrimes	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
3701558Srgrimes		blkerror(idesc->id_number, "BAD", blkno);
3711558Srgrimes		if (badblk++ >= MAXBAD) {
3721558Srgrimes			pwarn("EXCESSIVE BAD BLKS I=%lu",
3731558Srgrimes				idesc->id_number);
3741558Srgrimes			if (preen)
3751558Srgrimes				printf(" (SKIPPING)\n");
37634266Sjulian			else if (reply("CONTINUE") == 0) {
37734266Sjulian				ckfini(0);
37823675Speter				exit(EEXIT);
37934266Sjulian			}
3801558Srgrimes			return (STOP);
3811558Srgrimes		}
3821558Srgrimes	}
3831558Srgrimes	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
3841558Srgrimes		if (anyout && chkrange(blkno, 1)) {
3851558Srgrimes			res = SKIP;
3861558Srgrimes		} else if (!testbmap(blkno)) {
3871558Srgrimes			n_blks++;
3881558Srgrimes			setbmap(blkno);
3891558Srgrimes		} else {
3901558Srgrimes			blkerror(idesc->id_number, "DUP", blkno);
3911558Srgrimes			if (dupblk++ >= MAXDUP) {
3921558Srgrimes				pwarn("EXCESSIVE DUP BLKS I=%lu",
3931558Srgrimes					idesc->id_number);
3941558Srgrimes				if (preen)
3951558Srgrimes					printf(" (SKIPPING)\n");
39634266Sjulian				else if (reply("CONTINUE") == 0) {
39734266Sjulian					ckfini(0);
39823675Speter					exit(EEXIT);
39934266Sjulian				}
4001558Srgrimes				return (STOP);
4011558Srgrimes			}
4021558Srgrimes			new = (struct dups *)malloc(sizeof(struct dups));
4031558Srgrimes			if (new == NULL) {
4041558Srgrimes				pfatal("DUP TABLE OVERFLOW.");
40534266Sjulian				if (reply("CONTINUE") == 0) {
40634266Sjulian					ckfini(0);
40723675Speter					exit(EEXIT);
40834266Sjulian				}
4091558Srgrimes				return (STOP);
4101558Srgrimes			}
4111558Srgrimes			new->dup = blkno;
4121558Srgrimes			if (muldup == 0) {
4131558Srgrimes				duplist = muldup = new;
4141558Srgrimes				new->next = 0;
4151558Srgrimes			} else {
4161558Srgrimes				new->next = muldup->next;
4171558Srgrimes				muldup->next = new;
4181558Srgrimes			}
4191558Srgrimes			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
4201558Srgrimes				if (dlp->dup == blkno)
4211558Srgrimes					break;
4221558Srgrimes			if (dlp == muldup && dlp->dup != blkno)
4231558Srgrimes				muldup = new;
4241558Srgrimes		}
4251558Srgrimes		/*
4261558Srgrimes		 * count the number of blocks found in id_entryno
4271558Srgrimes		 */
4281558Srgrimes		idesc->id_entryno++;
4291558Srgrimes	}
4301558Srgrimes	return (res);
4311558Srgrimes}
432