Home | History | Annotate | Line # | Download | only in fsck_ffs
pass1.c revision 1.31
      1 /*	$NetBSD: pass1.c,v 1.31 2004/01/03 10:11:41 dbj 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)pass1.c	8.6 (Berkeley) 4/28/95";
     36 #else
     37 __RCSID("$NetBSD: pass1.c,v 1.31 2004/01/03 10:11:41 dbj Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/param.h>
     42 #include <sys/time.h>
     43 
     44 #include <ufs/ufs/dinode.h>
     45 #include <ufs/ufs/dir.h>
     46 #include <ufs/ffs/fs.h>
     47 #include <ufs/ufs/ufs_bswap.h>
     48 #include <ufs/ffs/ffs_extern.h>
     49 
     50 #include <err.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 
     55 #include "fsck.h"
     56 #include "extern.h"
     57 #include "fsutil.h"
     58 
     59 static daddr_t badblk;
     60 static daddr_t dupblk;
     61 static void checkinode __P((ino_t, struct inodesc *));
     62 static ino_t lastino;
     63 
     64 void
     65 pass1()
     66 {
     67 	ino_t inumber, inosused;
     68 	int c;
     69 	daddr_t i, cgd;
     70 	struct inodesc idesc;
     71 	struct cg *cgp = cgrp;
     72 	struct inostat *info;
     73 	uint8_t *cp;
     74 
     75 	/*
     76 	 * Set file system reserved blocks in used block map.
     77 	 */
     78 	for (c = 0; c < sblock->fs_ncg; c++) {
     79 		cgd = cgdmin(sblock, c);
     80 		if (c == 0)
     81 			i = cgbase(sblock, c);
     82 		else
     83 			i = cgsblock(sblock, c);
     84 		for (; i < cgd; i++)
     85 			setbmap(i);
     86 	}
     87 	i = sblock->fs_csaddr;
     88 	cgd = i + howmany(sblock->fs_cssize, sblock->fs_fsize);
     89 	for (; i < cgd; i++)
     90 		setbmap(i);
     91 	/*
     92 	 * Find all allocated blocks.
     93 	 */
     94 	memset(&idesc, 0, sizeof(struct inodesc));
     95 	idesc.id_type = ADDR;
     96 	idesc.id_func = pass1check;
     97 	n_files = n_blks = 0;
     98 	for (c = 0; c < sblock->fs_ncg; c++) {
     99 		inumber = c * sblock->fs_ipg;
    100 		setinodebuf(inumber);
    101 		getblk(&cgblk, cgtod(sblock, c), sblock->fs_cgsize);
    102 		memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
    103 		if((doswap && !needswap) || (!doswap && needswap))
    104 			ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
    105 		if (is_ufs2)
    106 			inosused = cgp->cg_initediblk;
    107 		else
    108 			inosused = sblock->fs_ipg;
    109 		if (got_siginfo) {
    110 			printf("%s: phase 1: cyl group %d of %d (%d%%)\n",
    111 			    cdevname(), c, sblock->fs_ncg,
    112 			    c * 100 / sblock->fs_ncg);
    113 			got_siginfo = 0;
    114 		}
    115 		/*
    116 		 * If we are using soft updates, then we can trust the
    117 		 * cylinder group inode allocation maps to tell us which
    118 		 * inodes are allocated. We will scan the used inode map
    119 		 * to find the inodes that are really in use, and then
    120 		 * read only those inodes in from disk.
    121 		 */
    122 		if (preen && usedsoftdep) {
    123 			if (!cg_chkmagic(cgp, 0))
    124 				pfatal("CG %d: BAD MAGIC NUMBER\n", c);
    125 			cp = &cg_inosused(cgp, 0)[(inosused - 1) / CHAR_BIT];
    126 			for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
    127 				if (*cp == 0)
    128 					continue;
    129 				for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
    130 					if (*cp & i)
    131 						break;
    132 					inosused--;
    133 				}
    134 				break;
    135 			}
    136 			if (inosused < 0)
    137 				inosused = 0;
    138 		}
    139 		/*
    140 		 * Allocate inoinfo structures for the allocated inodes.
    141 		 */
    142 		inostathead[c].il_numalloced = inosused;
    143 		if (inosused == 0) {
    144 			inostathead[c].il_stat = 0;
    145 			continue;
    146 		}
    147 		info = calloc((unsigned)inosused, sizeof(struct inostat));
    148 		if (info == NULL) {
    149 			pfatal("cannot alloc %u bytes for inoinfo\n",
    150 			    (unsigned)(sizeof(struct inostat) * inosused));
    151 			abort();
    152 		}
    153 		inostathead[c].il_stat = info;
    154 		/*
    155 		 * Scan the allocated inodes.
    156 		 */
    157 		for (i = 0; i < inosused; i++, inumber++) {
    158 			if (inumber < ROOTINO) {
    159 				(void)getnextinode(inumber);
    160 				continue;
    161 			}
    162 			checkinode(inumber, &idesc);
    163 		}
    164 		lastino += 1;
    165 		if (inosused < sblock->fs_ipg || inumber == lastino)
    166 			continue;
    167 		/*
    168 		 * If we were not able to determine in advance which inodes
    169 		 * were in use, then reduce the size of the inoinfo structure
    170 		 * to the size necessary to describe the inodes that we
    171 		 * really found.
    172 		 */
    173 		if (lastino < (c * sblock->fs_ipg))
    174 			inosused = 0;
    175 		else
    176 			inosused = lastino - (c * sblock->fs_ipg);
    177 		inostathead[c].il_numalloced = inosused;
    178 		if (inosused == 0) {
    179 			free(inostathead[c].il_stat);
    180 			inostathead[c].il_stat = 0;
    181 			continue;
    182 		}
    183 		info = calloc((unsigned)inosused, sizeof(struct inostat));
    184 		if (info == NULL) {
    185 			pfatal("cannot alloc %u bytes for inoinfo\n",
    186 			    (unsigned)(sizeof(struct inostat) * inosused));
    187 			abort();
    188 		}
    189 		memmove(info, inostathead[c].il_stat, inosused * sizeof(*info));
    190 		free(inostathead[c].il_stat);
    191 		inostathead[c].il_stat = info;
    192 	}
    193 	freeinodebuf();
    194 	do_blkswap = 0; /* has been done */
    195 }
    196 
    197 static void
    198 checkinode(inumber, idesc)
    199 	ino_t inumber;
    200 	struct inodesc *idesc;
    201 {
    202 	union dinode *dp;
    203 	struct zlncnt *zlnp;
    204 	daddr_t ndb;
    205 	int j;
    206 	mode_t mode;
    207 	u_int64_t size, kernmaxfilesize;
    208 	int64_t blocks;
    209 	char symbuf[MAXBSIZE];
    210 	struct inostat *info;
    211 
    212 	dp = getnextinode(inumber);
    213 	info = inoinfo(inumber);
    214 	mode = iswap16(DIP(dp, mode)) & IFMT;
    215 	size = iswap64(DIP(dp, size));
    216 	if (mode == 0) {
    217 		if ((is_ufs2 &&
    218 		    (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
    219 			NDADDR * sizeof(int64_t)) ||
    220 		    memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
    221 			NIADDR * sizeof(int64_t))))
    222 		    ||
    223 		    (!is_ufs2 &&
    224 		    (memcmp(dp->dp1.di_db, ufs1_zino.di_db,
    225 			NDADDR * sizeof(int32_t)) ||
    226 		    memcmp(dp->dp1.di_ib, ufs1_zino.di_ib,
    227 			NIADDR * sizeof(int32_t)))) ||
    228 		    mode || size) {
    229 			pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
    230 			if (reply("CLEAR") == 1) {
    231 				dp = ginode(inumber);
    232 				clearinode(dp);
    233 				inodirty();
    234 			} else
    235 				markclean = 0;
    236 		}
    237 		info->ino_state = USTATE;
    238 		return;
    239 	}
    240 	lastino = inumber;
    241 	/* This should match the file size limit in ffs_mountfs(). */
    242 	if (is_ufs2)
    243 		kernmaxfilesize = sblock->fs_maxfilesize;
    244 	else
    245 		kernmaxfilesize = (u_int64_t)0x80000000 * sblock->fs_bsize - 1;
    246 	if (size > kernmaxfilesize  || size + sblock->fs_bsize - 1 < size ||
    247 	    (mode == IFDIR && size > MAXDIRSIZE)) {
    248 		if (debug)
    249 			printf("bad size %llu:",(unsigned long long)size);
    250 		goto unknown;
    251 	}
    252 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
    253 		dp = ginode(inumber);
    254 		DIP(dp, size) = iswap64(sblock->fs_fsize);
    255 		size = sblock->fs_fsize;
    256 		DIP(dp, mode) = iswap16(IFREG|0600);
    257 		inodirty();
    258 	}
    259 	ndb = howmany(size, sblock->fs_bsize);
    260 	if (ndb < 0) {
    261 		if (debug)
    262 			printf("bad size %llu ndb %lld:",
    263 				(unsigned long long)size, (long long)ndb);
    264 		goto unknown;
    265 	}
    266 	if (mode == IFBLK || mode == IFCHR)
    267 		ndb++;
    268 	if (mode == IFLNK) {
    269 		/*
    270 		 * Note that the old fastlink format always had di_blocks set
    271 		 * to 0.  Other than that we no longer use the `spare' field
    272 		 * (which is now the extended uid) for sanity checking, the
    273 		 * new format is the same as the old.  We simply ignore the
    274 		 * conversion altogether.  - mycroft, 19MAY1994
    275 		 */
    276 		if (!is_ufs2 && doinglevel2 &&
    277 		    size > 0 && size < MAXSYMLINKLEN_UFS1 &&
    278 		    DIP(dp, blocks) != 0) {
    279 			if (bread(fsreadfd, symbuf,
    280 			    fsbtodb(sblock, iswap32(DIP(dp, db[0]))),
    281 			    (long)secsize) != 0)
    282 				errx(EEXIT, "cannot read symlink");
    283 			if (debug) {
    284 				symbuf[size] = 0;
    285 				printf("convert symlink %u(%s) of size %lld\n",
    286 				    inumber, symbuf,
    287 				    (unsigned long long)size);
    288 			}
    289 			dp = ginode(inumber);
    290 			memmove(dp->dp1.di_db, symbuf, (long)size);
    291 			DIP(dp, blocks) = 0;
    292 			inodirty();
    293 		}
    294 		/*
    295 		 * Fake ndb value so direct/indirect block checks below
    296 		 * will detect any garbage after symlink string.
    297 		 */
    298 		if ((sblock->fs_maxsymlinklen < 0) ||
    299 		    (size < sblock->fs_maxsymlinklen) ||
    300 		    (isappleufs && (size < APPLEUFS_MAXSYMLINKLEN)) ||
    301 		    (sblock->fs_maxsymlinklen == 0 && DIP(dp, blocks) == 0)) {
    302 			if (is_ufs2)
    303 				ndb = howmany(size, sizeof(int64_t));
    304 			else
    305 				ndb = howmany(size, sizeof(int32_t));
    306 			if (ndb > NDADDR) {
    307 				j = ndb - NDADDR;
    308 				for (ndb = 1; j > 1; j--)
    309 					ndb *= NINDIR(sblock);
    310 				ndb += NDADDR;
    311 			}
    312 		}
    313 	}
    314 	for (j = ndb; j < NDADDR; j++)
    315 		if (DIP(dp, db[j]) != 0) {
    316 		    if (debug) {
    317 			if (!is_ufs2)
    318 			    printf("bad direct addr ix %d: %d [ndb %lld]\n",
    319 				j, iswap32(dp->dp1.di_db[j]),
    320 				(long long)ndb);
    321 			else
    322 			    printf("bad direct addr ix %d: %lld [ndb %lld]\n",
    323 				j, (long long)iswap64(dp->dp2.di_db[j]),
    324 				(long long)ndb);
    325 		    }
    326 		    goto unknown;
    327 		}
    328 
    329 	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
    330 		ndb /= NINDIR(sblock);
    331 
    332 	for (; j < NIADDR; j++)
    333 		if (DIP(dp, ib[j]) != 0) {
    334 		    if (debug) {
    335 			if (!is_ufs2)
    336 			    printf("bad indirect addr: %d\n",
    337 				iswap32(dp->dp1.di_ib[j]));
    338 			else
    339 			    printf("bad indirect addr: %lld\n",
    340 				(long long)iswap64(dp->dp2.di_ib[j]));
    341 		    }
    342 		    goto unknown;
    343 		}
    344 	if (ftypeok(dp) == 0)
    345 		goto unknown;
    346 	n_files++;
    347 	info->ino_linkcnt = iswap16(DIP(dp, nlink));
    348 	if (info->ino_linkcnt <= 0) {
    349 		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
    350 		if (zlnp == NULL) {
    351 			markclean = 0;
    352 			pfatal("LINK COUNT TABLE OVERFLOW");
    353 			if (reply("CONTINUE") == 0) {
    354 				ckfini();
    355 				exit(EEXIT);
    356 			}
    357 		} else {
    358 			zlnp->zlncnt = inumber;
    359 			zlnp->next = zlnhead;
    360 			zlnhead = zlnp;
    361 		}
    362 	}
    363 	if (mode == IFDIR) {
    364 		if (size == 0)
    365 			info->ino_state = DCLEAR;
    366 		else
    367 			info->ino_state = DSTATE;
    368 		cacheino(dp, inumber);
    369 		countdirs++;
    370 	} else
    371 		info->ino_state = FSTATE;
    372 	info->ino_type = IFTODT(mode);
    373 	if (!is_ufs2 && doinglevel2 &&
    374 	    (iswap16(dp->dp1.di_ouid) != (u_short)-1 ||
    375 		iswap16(dp->dp1.di_ogid) != (u_short)-1)) {
    376 		dp = ginode(inumber);
    377 		dp->dp1.di_uid = iswap32(iswap16(dp->dp1.di_ouid));
    378 		dp->dp1.di_ouid = iswap16(-1);
    379 		dp->dp1.di_gid = iswap32(iswap16(dp->dp1.di_ogid));
    380 		dp->dp1.di_ogid = iswap16(-1);
    381 		inodirty();
    382 	}
    383 	badblk = dupblk = 0;
    384 	idesc->id_number = inumber;
    385 	(void)ckinode(dp, idesc);
    386 	idesc->id_entryno *= btodb(sblock->fs_fsize);
    387 	if (is_ufs2)
    388 		blocks = iswap64(dp->dp2.di_blocks);
    389 	else
    390 		blocks = iswap32(dp->dp1.di_blocks);
    391 	if (blocks != idesc->id_entryno) {
    392 		pwarn("INCORRECT BLOCK COUNT I=%u (%lld should be %lld)",
    393 		    inumber, (long long)blocks, (long long)idesc->id_entryno);
    394 		if (preen)
    395 			printf(" (CORRECTED)\n");
    396 		else if (reply("CORRECT") == 0) {
    397 			markclean = 0;
    398 			return;
    399 		}
    400 		dp = ginode(inumber);
    401 		if (is_ufs2)
    402 			dp->dp2.di_blocks = iswap64(idesc->id_entryno);
    403 		else
    404 			dp->dp1.di_blocks = iswap32((int32_t)idesc->id_entryno);
    405 		inodirty();
    406 	}
    407 	return;
    408 unknown:
    409 	pfatal("UNKNOWN FILE TYPE I=%u", inumber);
    410 	info->ino_state = FCLEAR;
    411 	if (reply("CLEAR") == 1) {
    412 		info->ino_state = USTATE;
    413 		dp = ginode(inumber);
    414 		clearinode(dp);
    415 		inodirty();
    416 	} else
    417 		markclean = 0;
    418 }
    419 
    420 int
    421 pass1check(idesc)
    422 	struct inodesc *idesc;
    423 {
    424 	int res = KEEPON;
    425 	int anyout, nfrags;
    426 	daddr_t blkno = idesc->id_blkno;
    427 	struct dups *dlp;
    428 	struct dups *new;
    429 
    430 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
    431 		blkerror(idesc->id_number, "BAD", blkno);
    432 		if (badblk++ >= MAXBAD) {
    433 			pwarn("EXCESSIVE BAD BLKS I=%u",
    434 				idesc->id_number);
    435 			if (preen)
    436 				printf(" (SKIPPING)\n");
    437 			else if (reply("CONTINUE") == 0) {
    438 				markclean = 0;
    439 				ckfini();
    440 				exit(EEXIT);
    441 			}
    442 			return (STOP);
    443 		}
    444 	}
    445 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
    446 		if (anyout && chkrange(blkno, 1)) {
    447 			res = SKIP;
    448 		} else if (!testbmap(blkno)) {
    449 			n_blks++;
    450 			setbmap(blkno);
    451 		} else {
    452 			blkerror(idesc->id_number, "DUP", blkno);
    453 			if (dupblk++ >= MAXDUP) {
    454 				pwarn("EXCESSIVE DUP BLKS I=%u",
    455 					idesc->id_number);
    456 				if (preen)
    457 					printf(" (SKIPPING)\n");
    458 				else if (reply("CONTINUE") == 0) {
    459 					markclean = 0;
    460 					ckfini();
    461 					exit(EEXIT);
    462 				}
    463 				return (STOP);
    464 			}
    465 			new = (struct dups *)malloc(sizeof(struct dups));
    466 			if (new == NULL) {
    467 				markclean = 0;
    468 				pfatal("DUP TABLE OVERFLOW.");
    469 				if (reply("CONTINUE") == 0) {
    470 					markclean = 0;
    471 					ckfini();
    472 					exit(EEXIT);
    473 				}
    474 				return (STOP);
    475 			}
    476 			new->dup = blkno;
    477 			if (muldup == 0) {
    478 				duplist = muldup = new;
    479 				new->next = 0;
    480 			} else {
    481 				new->next = muldup->next;
    482 				muldup->next = new;
    483 			}
    484 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
    485 				if (dlp->dup == blkno)
    486 					break;
    487 			if (dlp == muldup && dlp->dup != blkno)
    488 				muldup = new;
    489 		}
    490 		/*
    491 		 * count the number of blocks found in id_entryno
    492 		 */
    493 		idesc->id_entryno++;
    494 	}
    495 	return (res);
    496 }
    497