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