Home | History | Annotate | Line # | Download | only in fsck_ffs
pass1.c revision 1.21
      1 /*	$NetBSD: pass1.c,v 1.21 1998/03/18 17:01:24 bouyer 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.21 1998/03/18 17:01:24 bouyer 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 %qu:",(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 %qu 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 %qd\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: %d\n", iswap32(dp->di_db[j]));
    209 			goto unknown;
    210 		}
    211 	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
    212 		ndb /= NINDIR(sblock);
    213 	for (; j < NIADDR; j++)
    214 		if (dp->di_ib[j] != 0) {
    215 			if (debug)
    216 				printf("bad indirect addr: %d\n",
    217 					iswap32(dp->di_ib[j]));
    218 			goto unknown;
    219 		}
    220 	if (ftypeok(dp) == 0)
    221 		goto unknown;
    222 	n_files++;
    223 	lncntp[inumber] = iswap16(dp->di_nlink);
    224 	if (lncntp[inumber] <= 0) {
    225 		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
    226 		if (zlnp == NULL) {
    227 			markclean = 0;
    228 			pfatal("LINK COUNT TABLE OVERFLOW");
    229 			if (reply("CONTINUE") == 0)
    230 				exit(EEXIT);
    231 		} else {
    232 			zlnp->zlncnt = inumber;
    233 			zlnp->next = zlnhead;
    234 			zlnhead = zlnp;
    235 		}
    236 	}
    237 	if (mode == IFDIR) {
    238 		if (size == 0)
    239 			statemap[inumber] = DCLEAR;
    240 		else
    241 			statemap[inumber] = DSTATE;
    242 		cacheino(dp, inumber);
    243 	} else
    244 		statemap[inumber] = FSTATE;
    245 	typemap[inumber] = IFTODT(mode);
    246 	if (doinglevel2 &&
    247 	    (iswap16(dp->di_ouid) != (u_short)-1 ||
    248 		iswap16(dp->di_ogid) != (u_short)-1)) {
    249 		dp = ginode(inumber);
    250 		dp->di_uid = iswap32(iswap16(dp->di_ouid));
    251 		dp->di_ouid = iswap16(-1);
    252 		dp->di_gid = iswap32(iswap16(dp->di_ogid));
    253 		dp->di_ogid = iswap16(-1);
    254 		inodirty();
    255 	}
    256 	badblk = dupblk = 0;
    257 	idesc->id_number = inumber;
    258 	(void)ckinode(dp, idesc);
    259 	idesc->id_entryno *= btodb(sblock->fs_fsize);
    260 	if (iswap32(dp->di_blocks) != idesc->id_entryno) {
    261 		pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
    262 		    inumber, iswap32(dp->di_blocks), idesc->id_entryno);
    263 		if (preen)
    264 			printf(" (CORRECTED)\n");
    265 		else if (reply("CORRECT") == 0) {
    266 			markclean = 0;
    267 			return;
    268 		}
    269 		dp = ginode(inumber);
    270 		dp->di_blocks = iswap32(idesc->id_entryno);
    271 		inodirty();
    272 	}
    273 	return;
    274 unknown:
    275 	pfatal("UNKNOWN FILE TYPE I=%u", inumber);
    276 	statemap[inumber] = FCLEAR;
    277 	if (reply("CLEAR") == 1) {
    278 		statemap[inumber] = USTATE;
    279 		dp = ginode(inumber);
    280 		clearinode(dp);
    281 		inodirty();
    282 	} else
    283 		markclean = 0;
    284 }
    285 
    286 int
    287 pass1check(idesc)
    288 	struct inodesc *idesc;
    289 {
    290 	int res = KEEPON;
    291 	int anyout, nfrags;
    292 	ufs_daddr_t blkno = idesc->id_blkno;
    293 	struct dups *dlp;
    294 	struct dups *new;
    295 
    296 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
    297 		blkerror(idesc->id_number, "BAD", blkno);
    298 		if (badblk++ >= MAXBAD) {
    299 			pwarn("EXCESSIVE BAD BLKS I=%u",
    300 				idesc->id_number);
    301 			if (preen)
    302 				printf(" (SKIPPING)\n");
    303 			else if (reply("CONTINUE") == 0)
    304 				exit(EEXIT);
    305 			return (STOP);
    306 		}
    307 	}
    308 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
    309 		if (anyout && chkrange(blkno, 1)) {
    310 			res = SKIP;
    311 		} else if (!testbmap(blkno)) {
    312 			n_blks++;
    313 			setbmap(blkno);
    314 		} else {
    315 			blkerror(idesc->id_number, "DUP", blkno);
    316 			if (dupblk++ >= MAXDUP) {
    317 				pwarn("EXCESSIVE DUP BLKS I=%u",
    318 					idesc->id_number);
    319 				if (preen)
    320 					printf(" (SKIPPING)\n");
    321 				else if (reply("CONTINUE") == 0)
    322 					exit(EEXIT);
    323 				return (STOP);
    324 			}
    325 			new = (struct dups *)malloc(sizeof(struct dups));
    326 			if (new == NULL) {
    327 				markclean = 0;
    328 				pfatal("DUP TABLE OVERFLOW.");
    329 				if (reply("CONTINUE") == 0)
    330 					exit(EEXIT);
    331 				return (STOP);
    332 			}
    333 			new->dup = blkno;
    334 			if (muldup == 0) {
    335 				duplist = muldup = new;
    336 				new->next = 0;
    337 			} else {
    338 				new->next = muldup->next;
    339 				muldup->next = new;
    340 			}
    341 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
    342 				if (dlp->dup == blkno)
    343 					break;
    344 			if (dlp == muldup && dlp->dup != blkno)
    345 				muldup = new;
    346 		}
    347 		/*
    348 		 * count the number of blocks found in id_entryno
    349 		 */
    350 		idesc->id_entryno++;
    351 	}
    352 	return (res);
    353 }
    354