Home | History | Annotate | Line # | Download | only in fsck_ffs
pass1.c revision 1.23
      1 /*	$NetBSD: pass1.c,v 1.23 2001/01/05 02:02:57 lukem 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.23 2001/01/05 02:02:57 lukem 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 ufs_daddr_t badblk;
     62 static ufs_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 		for (i = 0; i < sblock->fs_ipg; i++, inumber++) {
     99 			if (inumber < ROOTINO)
    100 				continue;
    101 			checkinode(inumber, &idesc);
    102 		}
    103 	}
    104 	freeinodebuf();
    105 	do_blkswap = 0; /* has been done */
    106 }
    107 
    108 static void
    109 checkinode(inumber, idesc)
    110 	ino_t inumber;
    111 	struct inodesc *idesc;
    112 {
    113 	struct dinode *dp;
    114 	struct zlncnt *zlnp;
    115 	int ndb, j;
    116 	mode_t mode;
    117 	u_int64_t size;
    118 	char symbuf[MAXSYMLINKLEN];
    119 
    120 	dp = getnextinode(inumber);
    121 	mode = iswap16(dp->di_mode) & IFMT;
    122 	size = iswap64(dp->di_size);
    123 	if (mode == 0) {
    124 		if (memcmp(dp->di_db, zino.di_db,
    125 			NDADDR * sizeof(ufs_daddr_t)) ||
    126 		    memcmp(dp->di_ib, zino.di_ib,
    127 			NIADDR * sizeof(ufs_daddr_t)) ||
    128 		    dp->di_mode || dp->di_size) {
    129 			pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
    130 			if (reply("CLEAR") == 1) {
    131 				dp = ginode(inumber);
    132 				clearinode(dp);
    133 				inodirty();
    134 			} else
    135 				markclean = 0;
    136 		}
    137 		statemap[inumber] = USTATE;
    138 		return;
    139 	}
    140 	lastino = inumber;
    141 	if (/* dp->di_size < 0 || */
    142 	    size + sblock->fs_bsize - 1 < size ||
    143 	    (mode == IFDIR && size > MAXDIRSIZE)) {
    144 		if (debug)
    145 			printf("bad size %llu:",(unsigned long long)size);
    146 		goto unknown;
    147 	}
    148 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
    149 		dp = ginode(inumber);
    150 		dp->di_size = iswap64(sblock->fs_fsize);
    151 		size = sblock->fs_fsize;
    152 		dp->di_mode = iswap16(IFREG|0600);
    153 		inodirty();
    154 	}
    155 	ndb = howmany(size, sblock->fs_bsize);
    156 	if (ndb < 0) {
    157 		if (debug)
    158 			printf("bad size %llu ndb %d:",
    159 				(unsigned long long)size, ndb);
    160 		goto unknown;
    161 	}
    162 	if (mode == IFBLK || mode == IFCHR)
    163 		ndb++;
    164 	if (mode == IFLNK) {
    165 		/*
    166 		 * Note that the old fastlink format always had di_blocks set
    167 		 * to 0.  Other than that we no longer use the `spare' field
    168 		 * (which is now the extended uid) for sanity checking, the
    169 		 * new format is the same as the old.  We simply ignore the
    170 		 * conversion altogether.  - mycroft, 19MAY1994
    171 		 */
    172 		if (doinglevel2 &&
    173 		    size > 0 && size < MAXSYMLINKLEN &&
    174 		    dp->di_blocks != 0) {
    175 			if (bread(fsreadfd, symbuf,
    176 			    fsbtodb(sblock, iswap32(dp->di_db[0])),
    177 			    (long)secsize) != 0)
    178 				errx(EEXIT, "cannot read symlink");
    179 			if (debug) {
    180 				symbuf[size] = 0;
    181 				printf("convert symlink %u(%s) of size %lld\n",
    182 				    inumber, symbuf,
    183 				    (unsigned long long)size);
    184 			}
    185 			dp = ginode(inumber);
    186 			memmove(dp->di_shortlink, symbuf, (long)size);
    187 			dp->di_blocks = 0;
    188 			inodirty();
    189 		}
    190 		/*
    191 		 * Fake ndb value so direct/indirect block checks below
    192 		 * will detect any garbage after symlink string.
    193 		 */
    194 		if (size < sblock->fs_maxsymlinklen ||
    195 		    (sblock->fs_maxsymlinklen == 0 && dp->di_blocks == 0)) {
    196 			ndb = howmany(size, sizeof(daddr_t));
    197 			if (ndb > NDADDR) {
    198 				j = ndb - NDADDR;
    199 				for (ndb = 1; j > 1; j--)
    200 					ndb *= NINDIR(sblock);
    201 				ndb += NDADDR;
    202 			}
    203 		}
    204 	}
    205 	for (j = ndb; j < NDADDR; j++)
    206 		if (dp->di_db[j] != 0) {
    207 			if (debug)
    208 				printf("bad direct addr ix %d: %d [ndb %d]\n",
    209 					j, iswap32(dp->di_db[j]), ndb);
    210 			goto unknown;
    211 		}
    212 	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
    213 		ndb /= NINDIR(sblock);
    214 	for (; j < NIADDR; j++)
    215 		if (dp->di_ib[j] != 0) {
    216 			if (debug)
    217 				printf("bad indirect addr: %d\n",
    218 					iswap32(dp->di_ib[j]));
    219 			goto unknown;
    220 		}
    221 	if (ftypeok(dp) == 0)
    222 		goto unknown;
    223 	n_files++;
    224 	lncntp[inumber] = iswap16(dp->di_nlink);
    225 	if (lncntp[inumber] <= 0) {
    226 		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
    227 		if (zlnp == NULL) {
    228 			markclean = 0;
    229 			pfatal("LINK COUNT TABLE OVERFLOW");
    230 			if (reply("CONTINUE") == 0) {
    231 				ckfini();
    232 				exit(EEXIT);
    233 			}
    234 		} else {
    235 			zlnp->zlncnt = inumber;
    236 			zlnp->next = zlnhead;
    237 			zlnhead = zlnp;
    238 		}
    239 	}
    240 	if (mode == IFDIR) {
    241 		if (size == 0)
    242 			statemap[inumber] = DCLEAR;
    243 		else
    244 			statemap[inumber] = DSTATE;
    245 		cacheino(dp, inumber);
    246 	} else
    247 		statemap[inumber] = FSTATE;
    248 	typemap[inumber] = IFTODT(mode);
    249 	if (doinglevel2 &&
    250 	    (iswap16(dp->di_ouid) != (u_short)-1 ||
    251 		iswap16(dp->di_ogid) != (u_short)-1)) {
    252 		dp = ginode(inumber);
    253 		dp->di_uid = iswap32(iswap16(dp->di_ouid));
    254 		dp->di_ouid = iswap16(-1);
    255 		dp->di_gid = iswap32(iswap16(dp->di_ogid));
    256 		dp->di_ogid = iswap16(-1);
    257 		inodirty();
    258 	}
    259 	badblk = dupblk = 0;
    260 	idesc->id_number = inumber;
    261 	(void)ckinode(dp, idesc);
    262 	idesc->id_entryno *= btodb(sblock->fs_fsize);
    263 	if (iswap32(dp->di_blocks) != idesc->id_entryno) {
    264 		pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
    265 		    inumber, iswap32(dp->di_blocks), idesc->id_entryno);
    266 		if (preen)
    267 			printf(" (CORRECTED)\n");
    268 		else if (reply("CORRECT") == 0) {
    269 			markclean = 0;
    270 			return;
    271 		}
    272 		dp = ginode(inumber);
    273 		dp->di_blocks = iswap32(idesc->id_entryno);
    274 		inodirty();
    275 	}
    276 	return;
    277 unknown:
    278 	pfatal("UNKNOWN FILE TYPE I=%u", inumber);
    279 	statemap[inumber] = FCLEAR;
    280 	if (reply("CLEAR") == 1) {
    281 		statemap[inumber] = USTATE;
    282 		dp = ginode(inumber);
    283 		clearinode(dp);
    284 		inodirty();
    285 	} else
    286 		markclean = 0;
    287 }
    288 
    289 int
    290 pass1check(idesc)
    291 	struct inodesc *idesc;
    292 {
    293 	int res = KEEPON;
    294 	int anyout, nfrags;
    295 	ufs_daddr_t blkno = idesc->id_blkno;
    296 	struct dups *dlp;
    297 	struct dups *new;
    298 
    299 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
    300 		blkerror(idesc->id_number, "BAD", blkno);
    301 		if (badblk++ >= MAXBAD) {
    302 			pwarn("EXCESSIVE BAD BLKS I=%u",
    303 				idesc->id_number);
    304 			if (preen)
    305 				printf(" (SKIPPING)\n");
    306 			else if (reply("CONTINUE") == 0) {
    307 				markclean = 0;
    308 				ckfini();
    309 				exit(EEXIT);
    310 			}
    311 			return (STOP);
    312 		}
    313 	}
    314 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
    315 		if (anyout && chkrange(blkno, 1)) {
    316 			res = SKIP;
    317 		} else if (!testbmap(blkno)) {
    318 			n_blks++;
    319 			setbmap(blkno);
    320 		} else {
    321 			blkerror(idesc->id_number, "DUP", blkno);
    322 			if (dupblk++ >= MAXDUP) {
    323 				pwarn("EXCESSIVE DUP BLKS I=%u",
    324 					idesc->id_number);
    325 				if (preen)
    326 					printf(" (SKIPPING)\n");
    327 				else if (reply("CONTINUE") == 0) {
    328 					markclean = 0;
    329 					ckfini();
    330 					exit(EEXIT);
    331 				}
    332 				return (STOP);
    333 			}
    334 			new = (struct dups *)malloc(sizeof(struct dups));
    335 			if (new == NULL) {
    336 				markclean = 0;
    337 				pfatal("DUP TABLE OVERFLOW.");
    338 				if (reply("CONTINUE") == 0) {
    339 					markclean = 0;
    340 					ckfini();
    341 					exit(EEXIT);
    342 				}
    343 				return (STOP);
    344 			}
    345 			new->dup = blkno;
    346 			if (muldup == 0) {
    347 				duplist = muldup = new;
    348 				new->next = 0;
    349 			} else {
    350 				new->next = muldup->next;
    351 				muldup->next = new;
    352 			}
    353 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
    354 				if (dlp->dup == blkno)
    355 					break;
    356 			if (dlp == muldup && dlp->dup != blkno)
    357 				muldup = new;
    358 		}
    359 		/*
    360 		 * count the number of blocks found in id_entryno
    361 		 */
    362 		idesc->id_entryno++;
    363 	}
    364 	return (res);
    365 }
    366