Home | History | Annotate | Line # | Download | only in fsck_ffs
pass1.c revision 1.17
      1 /*	$NetBSD: pass1.c,v 1.17 1997/09/14 14:36:33 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.1 (Berkeley) 6/5/93";
     40 #else
     41 __RCSID("$NetBSD: pass1.c,v 1.17 1997/09/14 14:36:33 lukem Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/time.h>
     47 #include <ufs/ufs/dinode.h>
     48 #include <ufs/ufs/dir.h>
     49 #include <ufs/ffs/fs.h>
     50 
     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 
     63 void
     64 pass1()
     65 {
     66 	ino_t inumber;
     67 	int c, i, cgd;
     68 	struct inodesc idesc;
     69 
     70 	/*
     71 	 * Set file system reserved blocks in used block map.
     72 	 */
     73 	for (c = 0; c < sblock.fs_ncg; c++) {
     74 		cgd = cgdmin(&sblock, c);
     75 		if (c == 0)
     76 			i = cgbase(&sblock, c);
     77 		else
     78 			i = cgsblock(&sblock, c);
     79 		for (; i < cgd; i++)
     80 			setbmap(i);
     81 	}
     82 	i = sblock.fs_csaddr;
     83 	cgd = i + howmany(sblock.fs_cssize, sblock.fs_fsize);
     84 	for (; i < cgd; i++)
     85 		setbmap(i);
     86 	/*
     87 	 * Find all allocated blocks.
     88 	 */
     89 	memset(&idesc, 0, sizeof(struct inodesc));
     90 	idesc.id_type = ADDR;
     91 	idesc.id_func = pass1check;
     92 	inumber = 0;
     93 	n_files = n_blks = 0;
     94 	resetinodebuf();
     95 	for (c = 0; c < sblock.fs_ncg; c++) {
     96 		for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
     97 			if (inumber < ROOTINO)
     98 				continue;
     99 			checkinode(inumber, &idesc);
    100 		}
    101 	}
    102 	freeinodebuf();
    103 }
    104 
    105 static void
    106 checkinode(inumber, idesc)
    107 	ino_t inumber;
    108 	struct inodesc *idesc;
    109 {
    110 	struct dinode *dp;
    111 	struct zlncnt *zlnp;
    112 	int ndb, j;
    113 	mode_t mode;
    114 	char *symbuf;
    115 
    116 	dp = getnextinode(inumber);
    117 	mode = dp->di_mode & IFMT;
    118 	if (mode == 0) {
    119 		if (memcmp(dp->di_db, zino.di_db, NDADDR * sizeof(daddr_t)) ||
    120 		    memcmp(dp->di_ib, zino.di_ib, NIADDR * sizeof(daddr_t)) ||
    121 		    dp->di_mode || dp->di_size) {
    122 			pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
    123 			if (reply("CLEAR") == 1) {
    124 				dp = ginode(inumber);
    125 				clearinode(dp);
    126 				inodirty();
    127 			}
    128 		}
    129 		statemap[inumber] = USTATE;
    130 		return;
    131 	}
    132 	lastino = inumber;
    133 	if (/* dp->di_size < 0 || */
    134 	    dp->di_size + sblock.fs_bsize - 1 < dp->di_size) {
    135 		if (debug)
    136 			printf("bad size %qu:", dp->di_size);
    137 		goto unknown;
    138 	}
    139 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
    140 		dp = ginode(inumber);
    141 		dp->di_size = sblock.fs_fsize;
    142 		dp->di_mode = IFREG|0600;
    143 		inodirty();
    144 	}
    145 	ndb = howmany(dp->di_size, sblock.fs_bsize);
    146 	if (ndb < 0) {
    147 		if (debug)
    148 			printf("bad size %qu ndb %d:",
    149 				dp->di_size, ndb);
    150 		goto unknown;
    151 	}
    152 	if (mode == IFBLK || mode == IFCHR)
    153 		ndb++;
    154 	if (mode == IFLNK) {
    155 		/*
    156 		 * Note that the old fastlink format always had di_blocks set
    157 		 * to 0.  Other than that we no longer use the `spare' field
    158 		 * (which is now the extended uid) for sanity checking, the
    159 		 * new format is the same as the old.  We simply ignore the
    160 		 * conversion altogether.  - mycroft, 19MAY1994
    161 		 */
    162 		if (doinglevel2 &&
    163 		    dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
    164 		    dp->di_blocks != 0) {
    165 			symbuf = alloca(secsize);
    166 			if (bread(fsreadfd, symbuf,
    167 			    fsbtodb(&sblock, dp->di_db[0]),
    168 			    (long)secsize) != 0)
    169 				errexit("cannot read symlink");
    170 			if (debug) {
    171 				symbuf[dp->di_size] = 0;
    172 				printf("convert symlink %d(%s) of size %qd\n",
    173 					inumber, symbuf, dp->di_size);
    174 			}
    175 			dp = ginode(inumber);
    176 			memcpy(dp->di_shortlink, symbuf, (long)dp->di_size);
    177 			dp->di_blocks = 0;
    178 			inodirty();
    179 		}
    180 		/*
    181 		 * Fake ndb value so direct/indirect block checks below
    182 		 * will detect any garbage after symlink string.
    183 		 */
    184 		if (dp->di_size < sblock.fs_maxsymlinklen ||
    185 		    (sblock.fs_maxsymlinklen == 0 && dp->di_blocks == 0)) {
    186 			ndb = howmany(dp->di_size, sizeof(daddr_t));
    187 			if (ndb > NDADDR) {
    188 				j = ndb - NDADDR;
    189 				for (ndb = 1; j > 1; j--)
    190 					ndb *= NINDIR(&sblock);
    191 				ndb += NDADDR;
    192 			}
    193 		}
    194 	}
    195 	for (j = ndb; j < NDADDR; j++)
    196 		if (dp->di_db[j] != 0) {
    197 			if (debug)
    198 				printf("bad direct addr: %d\n", dp->di_db[j]);
    199 			goto unknown;
    200 		}
    201 	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
    202 		ndb /= NINDIR(&sblock);
    203 	for (; j < NIADDR; j++)
    204 		if (dp->di_ib[j] != 0) {
    205 			if (debug)
    206 				printf("bad indirect addr: %d\n",
    207 					dp->di_ib[j]);
    208 			goto unknown;
    209 		}
    210 	if (ftypeok(dp) == 0)
    211 		goto unknown;
    212 	n_files++;
    213 	lncntp[inumber] = dp->di_nlink;
    214 	if (dp->di_nlink <= 0) {
    215 		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
    216 		if (zlnp == NULL) {
    217 			pfatal("LINK COUNT TABLE OVERFLOW");
    218 			if (reply("CONTINUE") == 0)
    219 				errexit("%s", "");
    220 		} else {
    221 			zlnp->zlncnt = inumber;
    222 			zlnp->next = zlnhead;
    223 			zlnhead = zlnp;
    224 		}
    225 	}
    226 	if (mode == IFDIR) {
    227 		if (dp->di_size == 0)
    228 			statemap[inumber] = DCLEAR;
    229 		else
    230 			statemap[inumber] = DSTATE;
    231 		cacheino(dp, inumber);
    232 	} else
    233 		statemap[inumber] = FSTATE;
    234 	typemap[inumber] = IFTODT(mode);
    235 	if (doinglevel2 &&
    236 	    (dp->di_ouid != (u_short)-1 || dp->di_ogid != (u_short)-1)) {
    237 		dp = ginode(inumber);
    238 		dp->di_uid = dp->di_ouid;
    239 		dp->di_ouid = -1;
    240 		dp->di_gid = dp->di_ogid;
    241 		dp->di_ogid = -1;
    242 		inodirty();
    243 	}
    244 	badblk = dupblk = 0;
    245 	idesc->id_number = inumber;
    246 	(void)ckinode(dp, idesc);
    247 	idesc->id_entryno *= btodb(sblock.fs_fsize);
    248 	if (dp->di_blocks != idesc->id_entryno) {
    249 		pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
    250 		    inumber, dp->di_blocks, idesc->id_entryno);
    251 		if (preen)
    252 			printf(" (CORRECTED)\n");
    253 		else if (reply("CORRECT") == 0)
    254 			return;
    255 		dp = ginode(inumber);
    256 		dp->di_blocks = idesc->id_entryno;
    257 		inodirty();
    258 	}
    259 	return;
    260 unknown:
    261 	pfatal("UNKNOWN FILE TYPE I=%u", inumber);
    262 	statemap[inumber] = FCLEAR;
    263 	if (reply("CLEAR") == 1) {
    264 		statemap[inumber] = USTATE;
    265 		dp = ginode(inumber);
    266 		clearinode(dp);
    267 		inodirty();
    268 	}
    269 }
    270 
    271 int
    272 pass1check(idesc)
    273 	struct inodesc *idesc;
    274 {
    275 	int res = KEEPON;
    276 	int anyout, nfrags;
    277 	daddr_t blkno = idesc->id_blkno;
    278 	struct dups *dlp;
    279 	struct dups *new;
    280 
    281 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
    282 		blkerror(idesc->id_number, "BAD", blkno);
    283 		if (badblk++ >= MAXBAD) {
    284 			pwarn("EXCESSIVE BAD BLKS I=%u",
    285 				idesc->id_number);
    286 			if (preen)
    287 				printf(" (SKIPPING)\n");
    288 			else if (reply("CONTINUE") == 0)
    289 				errexit("%s", "");
    290 			return (STOP);
    291 		}
    292 	}
    293 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
    294 		if (anyout && chkrange(blkno, 1)) {
    295 			res = SKIP;
    296 		} else if (!testbmap(blkno)) {
    297 			n_blks++;
    298 			setbmap(blkno);
    299 		} else {
    300 			blkerror(idesc->id_number, "DUP", blkno);
    301 			if (dupblk++ >= MAXDUP) {
    302 				pwarn("EXCESSIVE DUP BLKS I=%u",
    303 					idesc->id_number);
    304 				if (preen)
    305 					printf(" (SKIPPING)\n");
    306 				else if (reply("CONTINUE") == 0)
    307 					errexit("%s", "");
    308 				return (STOP);
    309 			}
    310 			new = (struct dups *)malloc(sizeof(struct dups));
    311 			if (new == NULL) {
    312 				pfatal("DUP TABLE OVERFLOW.");
    313 				if (reply("CONTINUE") == 0)
    314 					errexit("%s", "");
    315 				return (STOP);
    316 			}
    317 			new->dup = blkno;
    318 			if (muldup == 0) {
    319 				duplist = muldup = new;
    320 				new->next = 0;
    321 			} else {
    322 				new->next = muldup->next;
    323 				muldup->next = new;
    324 			}
    325 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
    326 				if (dlp->dup == blkno)
    327 					break;
    328 			if (dlp == muldup && dlp->dup != blkno)
    329 				muldup = new;
    330 		}
    331 		/*
    332 		 * count the number of blocks found in id_entryno
    333 		 */
    334 		idesc->id_entryno++;
    335 	}
    336 	return (res);
    337 }
    338