Home | History | Annotate | Line # | Download | only in fsck_ffs
pass1.c revision 1.26
      1 /*	$NetBSD: pass1.c,v 1.26 2003/01/24 21:55:08 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1986, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)pass1.c	8.6 (Berkeley) 4/28/95";
     40 #else
     41 __RCSID("$NetBSD: pass1.c,v 1.26 2003/01/24 21:55:08 fvdl Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/time.h>
     47 
     48 #include <ufs/ufs/dinode.h>
     49 #include <ufs/ufs/dir.h>
     50 #include <ufs/ffs/fs.h>
     51 
     52 #include <err.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 
     57 #include "fsck.h"
     58 #include "extern.h"
     59 #include "fsutil.h"
     60 
     61 static daddr_t badblk;
     62 static daddr_t dupblk;
     63 static void checkinode __P((ino_t, struct inodesc *));
     64 
     65 void
     66 pass1()
     67 {
     68 	ino_t inumber;
     69 	int c, i, cgd;
     70 	struct inodesc idesc;
     71 
     72 	/*
     73 	 * Set file system reserved blocks in used block map.
     74 	 */
     75 	for (c = 0; c < sblock->fs_ncg; c++) {
     76 		cgd = cgdmin(sblock, c);
     77 		if (c == 0)
     78 			i = cgbase(sblock, c);
     79 		else
     80 			i = cgsblock(sblock, c);
     81 		for (; i < cgd; i++)
     82 			setbmap(i);
     83 	}
     84 	i = sblock->fs_csaddr;
     85 	cgd = i + howmany(sblock->fs_cssize, sblock->fs_fsize);
     86 	for (; i < cgd; i++)
     87 		setbmap(i);
     88 	/*
     89 	 * Find all allocated blocks.
     90 	 */
     91 	memset(&idesc, 0, sizeof(struct inodesc));
     92 	idesc.id_type = ADDR;
     93 	idesc.id_func = pass1check;
     94 	inumber = 0;
     95 	n_files = n_blks = 0;
     96 	resetinodebuf();
     97 	for (c = 0; c < sblock->fs_ncg; c++) {
     98 		if (got_siginfo) {
     99 			fprintf(stderr,
    100 			    "%s: phase 1: cyl group %d of %d (%d%%)\n",
    101 			    cdevname(), c, sblock->fs_ncg,
    102 			    c * 100 / sblock->fs_ncg);
    103 			got_siginfo = 0;
    104 		}
    105 		for (i = 0; i < sblock->fs_ipg; i++, inumber++) {
    106 			if (inumber < ROOTINO)
    107 				continue;
    108 			checkinode(inumber, &idesc);
    109 		}
    110 	}
    111 	freeinodebuf();
    112 	do_blkswap = 0; /* has been done */
    113 }
    114 
    115 static void
    116 checkinode(inumber, idesc)
    117 	ino_t inumber;
    118 	struct inodesc *idesc;
    119 {
    120 	struct dinode *dp;
    121 	struct zlncnt *zlnp;
    122 	int ndb, j;
    123 	mode_t mode;
    124 	u_int64_t size;
    125 	char symbuf[MAXSYMLINKLEN];
    126 
    127 	dp = getnextinode(inumber);
    128 	mode = iswap16(dp->di_mode) & IFMT;
    129 	size = iswap64(dp->di_size);
    130 	if (mode == 0) {
    131 		/* XXX ondisk32 */
    132 		if (memcmp(dp->di_db, zino.di_db,
    133 			NDADDR * sizeof(int32_t)) ||
    134 		    memcmp(dp->di_ib, zino.di_ib,
    135 			NIADDR * sizeof(int32_t)) ||
    136 		    dp->di_mode || dp->di_size) {
    137 			pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
    138 			if (reply("CLEAR") == 1) {
    139 				dp = ginode(inumber);
    140 				clearinode(dp);
    141 				inodirty();
    142 			} else
    143 				markclean = 0;
    144 		}
    145 		statemap[inumber] = USTATE;
    146 		return;
    147 	}
    148 	lastino = inumber;
    149 	if (/* dp->di_size < 0 || */
    150 	    size + sblock->fs_bsize - 1 < size ||
    151 	    (mode == IFDIR && size > MAXDIRSIZE)) {
    152 		if (debug)
    153 			printf("bad size %llu:",(unsigned long long)size);
    154 		goto unknown;
    155 	}
    156 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
    157 		dp = ginode(inumber);
    158 		dp->di_size = iswap64(sblock->fs_fsize);
    159 		size = sblock->fs_fsize;
    160 		dp->di_mode = iswap16(IFREG|0600);
    161 		inodirty();
    162 	}
    163 	ndb = howmany(size, sblock->fs_bsize);
    164 	if (ndb < 0) {
    165 		if (debug)
    166 			printf("bad size %llu ndb %d:",
    167 				(unsigned long long)size, ndb);
    168 		goto unknown;
    169 	}
    170 	if (mode == IFBLK || mode == IFCHR)
    171 		ndb++;
    172 	if (mode == IFLNK) {
    173 		/*
    174 		 * Note that the old fastlink format always had di_blocks set
    175 		 * to 0.  Other than that we no longer use the `spare' field
    176 		 * (which is now the extended uid) for sanity checking, the
    177 		 * new format is the same as the old.  We simply ignore the
    178 		 * conversion altogether.  - mycroft, 19MAY1994
    179 		 */
    180 		if (doinglevel2 &&
    181 		    size > 0 && size < MAXSYMLINKLEN &&
    182 		    dp->di_blocks != 0) {
    183 			if (bread(fsreadfd, symbuf,
    184 			    fsbtodb(sblock, iswap32(dp->di_db[0])),
    185 			    (long)secsize) != 0)
    186 				errx(EEXIT, "cannot read symlink");
    187 			if (debug) {
    188 				symbuf[size] = 0;
    189 				printf("convert symlink %u(%s) of size %lld\n",
    190 				    inumber, symbuf,
    191 				    (unsigned long long)size);
    192 			}
    193 			dp = ginode(inumber);
    194 			memmove(dp->di_shortlink, symbuf, (long)size);
    195 			dp->di_blocks = 0;
    196 			inodirty();
    197 		}
    198 		/*
    199 		 * Fake ndb value so direct/indirect block checks below
    200 		 * will detect any garbage after symlink string.
    201 		 */
    202 		if (size < sblock->fs_maxsymlinklen ||
    203 		    (isappleufs && (size < APPLEUFS_MAXSYMLINKLEN)) ||
    204 		    (sblock->fs_maxsymlinklen == 0 && dp->di_blocks == 0)) {
    205 			/* XXX ondisk32 */
    206 			ndb = howmany(size, sizeof(int32_t));
    207 			if (ndb > NDADDR) {
    208 				j = ndb - NDADDR;
    209 				for (ndb = 1; j > 1; j--)
    210 					ndb *= NINDIR(sblock);
    211 				ndb += NDADDR;
    212 			}
    213 		}
    214 	}
    215 	for (j = ndb; j < NDADDR; j++)
    216 		if (dp->di_db[j] != 0) {
    217 			if (debug)
    218 				printf("bad direct addr ix %d: %d [ndb %d]\n",
    219 					j, iswap32(dp->di_db[j]), ndb);
    220 			goto unknown;
    221 		}
    222 	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
    223 		ndb /= NINDIR(sblock);
    224 	for (; j < NIADDR; j++)
    225 		if (dp->di_ib[j] != 0) {
    226 			if (debug)
    227 				printf("bad indirect addr: %d\n",
    228 					iswap32(dp->di_ib[j]));
    229 			goto unknown;
    230 		}
    231 	if (ftypeok(dp) == 0)
    232 		goto unknown;
    233 	n_files++;
    234 	lncntp[inumber] = iswap16(dp->di_nlink);
    235 	if (lncntp[inumber] <= 0) {
    236 		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
    237 		if (zlnp == NULL) {
    238 			markclean = 0;
    239 			pfatal("LINK COUNT TABLE OVERFLOW");
    240 			if (reply("CONTINUE") == 0) {
    241 				ckfini();
    242 				exit(EEXIT);
    243 			}
    244 		} else {
    245 			zlnp->zlncnt = inumber;
    246 			zlnp->next = zlnhead;
    247 			zlnhead = zlnp;
    248 		}
    249 	}
    250 	if (mode == IFDIR) {
    251 		if (size == 0)
    252 			statemap[inumber] = DCLEAR;
    253 		else
    254 			statemap[inumber] = DSTATE;
    255 		cacheino(dp, inumber);
    256 	} else
    257 		statemap[inumber] = FSTATE;
    258 	typemap[inumber] = IFTODT(mode);
    259 	if (doinglevel2 &&
    260 	    (iswap16(dp->di_ouid) != (u_short)-1 ||
    261 		iswap16(dp->di_ogid) != (u_short)-1)) {
    262 		dp = ginode(inumber);
    263 		dp->di_uid = iswap32(iswap16(dp->di_ouid));
    264 		dp->di_ouid = iswap16(-1);
    265 		dp->di_gid = iswap32(iswap16(dp->di_ogid));
    266 		dp->di_ogid = iswap16(-1);
    267 		inodirty();
    268 	}
    269 	badblk = dupblk = 0;
    270 	idesc->id_number = inumber;
    271 	(void)ckinode(dp, idesc);
    272 	idesc->id_entryno *= btodb(sblock->fs_fsize);
    273 	if (iswap32(dp->di_blocks) != idesc->id_entryno) {
    274 		pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
    275 		    inumber, iswap32(dp->di_blocks), idesc->id_entryno);
    276 		if (preen)
    277 			printf(" (CORRECTED)\n");
    278 		else if (reply("CORRECT") == 0) {
    279 			markclean = 0;
    280 			return;
    281 		}
    282 		dp = ginode(inumber);
    283 		dp->di_blocks = iswap32(idesc->id_entryno);
    284 		inodirty();
    285 	}
    286 	return;
    287 unknown:
    288 	pfatal("UNKNOWN FILE TYPE I=%u", inumber);
    289 	statemap[inumber] = FCLEAR;
    290 	if (reply("CLEAR") == 1) {
    291 		statemap[inumber] = USTATE;
    292 		dp = ginode(inumber);
    293 		clearinode(dp);
    294 		inodirty();
    295 	} else
    296 		markclean = 0;
    297 }
    298 
    299 int
    300 pass1check(idesc)
    301 	struct inodesc *idesc;
    302 {
    303 	int res = KEEPON;
    304 	int anyout, nfrags;
    305 	daddr_t blkno = idesc->id_blkno;
    306 	struct dups *dlp;
    307 	struct dups *new;
    308 
    309 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
    310 		blkerror(idesc->id_number, "BAD", blkno);
    311 		if (badblk++ >= MAXBAD) {
    312 			pwarn("EXCESSIVE BAD BLKS I=%u",
    313 				idesc->id_number);
    314 			if (preen)
    315 				printf(" (SKIPPING)\n");
    316 			else if (reply("CONTINUE") == 0) {
    317 				markclean = 0;
    318 				ckfini();
    319 				exit(EEXIT);
    320 			}
    321 			return (STOP);
    322 		}
    323 	}
    324 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
    325 		if (anyout && chkrange(blkno, 1)) {
    326 			res = SKIP;
    327 		} else if (!testbmap(blkno)) {
    328 			n_blks++;
    329 			setbmap(blkno);
    330 		} else {
    331 			blkerror(idesc->id_number, "DUP", blkno);
    332 			if (dupblk++ >= MAXDUP) {
    333 				pwarn("EXCESSIVE DUP BLKS I=%u",
    334 					idesc->id_number);
    335 				if (preen)
    336 					printf(" (SKIPPING)\n");
    337 				else if (reply("CONTINUE") == 0) {
    338 					markclean = 0;
    339 					ckfini();
    340 					exit(EEXIT);
    341 				}
    342 				return (STOP);
    343 			}
    344 			new = (struct dups *)malloc(sizeof(struct dups));
    345 			if (new == NULL) {
    346 				markclean = 0;
    347 				pfatal("DUP TABLE OVERFLOW.");
    348 				if (reply("CONTINUE") == 0) {
    349 					markclean = 0;
    350 					ckfini();
    351 					exit(EEXIT);
    352 				}
    353 				return (STOP);
    354 			}
    355 			new->dup = blkno;
    356 			if (muldup == 0) {
    357 				duplist = muldup = new;
    358 				new->next = 0;
    359 			} else {
    360 				new->next = muldup->next;
    361 				muldup->next = new;
    362 			}
    363 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
    364 				if (dlp->dup == blkno)
    365 					break;
    366 			if (dlp == muldup && dlp->dup != blkno)
    367 				muldup = new;
    368 		}
    369 		/*
    370 		 * count the number of blocks found in id_entryno
    371 		 */
    372 		idesc->id_entryno++;
    373 	}
    374 	return (res);
    375 }
    376