Home | History | Annotate | Line # | Download | only in fsck_lfs
pass1.c revision 1.7
      1 /* $NetBSD: pass1.c,v 1.7 2000/05/30 04:33:15 perseant 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/param.h>
     37 #include <sys/time.h>
     38 #include <ufs/ufs/dinode.h>
     39 #include <ufs/ufs/dir.h>
     40 #include <sys/mount.h>
     41 #include <ufs/lfs/lfs.h>
     42 
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 
     47 #include <signal.h>
     48 
     49 #include "fsck.h"
     50 #include "extern.h"
     51 #include "fsutil.h"
     52 
     53 SEGUSE         *seg_table;
     54 extern daddr_t *din_table;
     55 
     56 static daddr_t  badblk;
     57 static daddr_t  dupblk;
     58 static void     checkinode(ino_t, struct inodesc *);
     59 static int      i_d_cmp(const void *, const void *);
     60 
     61 static void
     62 bmapcheck(void)
     63 {
     64 	int             i;
     65 
     66 	if (testbmap(0))
     67 		raise(1);
     68 	for (i = 0; i < maxfsblock; i++)
     69 		if (testbmap(i) > maxino)
     70 			raise(1);
     71 }
     72 
     73 struct ino_daddr {
     74 	ino_t           ino;
     75 	daddr_t         daddr;
     76 };
     77 
     78 static int
     79 i_d_cmp(const void *va, const void *vb)
     80 {
     81 	struct ino_daddr *a, *b;
     82 
     83 	a = *((struct ino_daddr **)va);
     84 	b = *((struct ino_daddr **)vb);
     85 
     86 	if (a->daddr == b->daddr) {
     87 		return (a->ino - b->ino);
     88 	}
     89 	if (a->daddr > b->daddr) {
     90 		return 1;
     91 	}
     92 	return -1;
     93 }
     94 
     95 void
     96 pass1()
     97 {
     98 	ino_t           inumber;
     99 	int             i, total_segments;
    100 	struct inodesc  idesc;
    101 	struct dinode  *idinode, *tinode;
    102 	struct ifile   *ifp;
    103 	CLEANERINFO    *cp;
    104 	struct bufarea *bp;
    105 	struct ino_daddr **dins;
    106 
    107 	idinode = lfs_difind(&sblock, sblock.lfs_ifile, &ifblock);
    108 
    109 	/*
    110          * We now have the ifile's inode block in core.  Read out the
    111          * number of segments.
    112          */
    113 #if 1
    114 	if (pbp != 0)
    115 		pbp->b_flags &= ~B_INUSE;
    116 	pbp = getddblk(idinode->di_db[0], sblock.lfs_bsize);
    117 
    118 	cp = (CLEANERINFO *)(pbp->b_un.b_buf);
    119 #endif
    120 	total_segments = sblock.lfs_size / sblock.lfs_bsize;
    121 
    122 	/*
    123 	 * Find all allocated blocks, initialize numdirs.
    124 	 */
    125 	memset(&idesc, 0, sizeof(struct inodesc));
    126 	idesc.id_type = ADDR;
    127 	idesc.id_func = pass1check;
    128 	idesc.id_lblkno = 0;
    129 	inumber = 0;
    130 	n_files = n_blks = 0;
    131 
    132 	if (debug)
    133 		printf("creating inode address table...\n");
    134 	/* Sort by daddr */
    135 	dins = (struct ino_daddr **)malloc((maxino + 1) * sizeof(*dins));
    136 	for (i = 0; i <= maxino; i++) {
    137 		dins[i] = malloc(sizeof(**dins));
    138 		dins[i]->ino = i;
    139 		dins[i]->daddr = lfs_ino_daddr(i);
    140 	}
    141 	qsort(dins, maxino + 1, sizeof(*dins), i_d_cmp);
    142 
    143 	/* find a value for numdirs, fill in din_table */
    144 	if (debug)
    145 		printf("counting dirs...\n");
    146 	numdirs = 0;
    147 	for (i = 0; i <= maxino; i++) {
    148 		inumber = dins[i]->ino;
    149 		if (inumber == 0 || dins[i]->daddr == 0)
    150 			continue;
    151 		tinode = lfs_ginode(inumber);
    152 		if (tinode && (tinode->di_mode & IFMT) == IFDIR)
    153 			numdirs++;
    154 	}
    155 
    156 	/* from setup.c */
    157 	inplast = 0;
    158 	listmax = numdirs + 10;
    159 	inpsort = (struct inoinfo **)calloc((unsigned) listmax,
    160 					     sizeof(struct inoinfo *));
    161 	inphead = (struct inoinfo **)calloc((unsigned) numdirs,
    162 					     sizeof(struct inoinfo *));
    163 	if (inpsort == NULL || inphead == NULL) {
    164 		printf("cannot alloc %lu bytes for inphead\n",
    165 		       (unsigned long)numdirs * sizeof(struct inoinfo *));
    166 		exit(1);
    167 	}
    168 	/* resetinodebuf(); */
    169 	if (debug)
    170 		printf("counting blocks...\n");
    171 	for (i = 0; i <= maxino; i++) {
    172 		inumber = dins[i]->ino;
    173 		if (inumber == 0 || dins[i]->daddr == 0) {
    174 			statemap[inumber] = USTATE;
    175 			continue;
    176 		}
    177 		ifp = lfs_ientry(inumber, &bp);
    178 		if (ifp && ifp->if_daddr != LFS_UNUSED_DADDR) {
    179 			bp->b_flags &= ~B_INUSE;
    180 			checkinode(inumber, &idesc);
    181 		} else {
    182 			bp->b_flags &= ~B_INUSE;
    183 			statemap[inumber] = USTATE;
    184 		}
    185 		free(dins[i]);
    186 	}
    187 	free(dins);
    188 
    189 	bmapcheck();
    190 	/* freeinodebuf(); */
    191 }
    192 
    193 static void
    194 checkinode(ino_t inumber, struct inodesc * idesc)
    195 {
    196 	register struct dinode *dp;
    197 	struct zlncnt  *zlnp;
    198 	int             ndb, j;
    199 	mode_t          mode;
    200 	char           *symbuf;
    201 
    202 	/* dp = getnextinode(inumber); */
    203 	dp = lfs_ginode(inumber);
    204 
    205 	if (dp == NULL) {
    206 		/* pwarn("Could not find inode %ld\n",(long)inumber); */
    207 		statemap[inumber] = USTATE;
    208 		return;
    209 	}
    210 	mode = dp->di_mode & IFMT;
    211 
    212 	/* XXX - LFS doesn't have this particular problem (?) */
    213 	if (mode == 0) {
    214 		if (memcmp(dp->di_db, zino.di_db, NDADDR * sizeof(daddr_t)) ||
    215 		  memcmp(dp->di_ib, zino.di_ib, NIADDR * sizeof(daddr_t)) ||
    216 		    dp->di_mode || dp->di_size) {
    217 			pwarn("mode=o%o, ifmt=o%o\n", dp->di_mode, mode);
    218 			pfatal("PARTIALLY ALLOCATED INODE I=%u", inumber);
    219 			if (reply("CLEAR") == 1) {
    220 				dp = ginode(inumber);
    221 				clearinode(dp);
    222 				inodirty();
    223 			}
    224 		}
    225 		statemap[inumber] = USTATE;
    226 		return;
    227 	}
    228 	lastino = inumber;
    229 	if (			/* dp->di_size < 0 || */
    230 	    dp->di_size + sblock.lfs_bsize - 1 < dp->di_size) {
    231 		if (debug)
    232 			printf("bad size %qu:", (unsigned long long)dp->di_size);
    233 		goto unknown;
    234 	}
    235 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
    236 		dp = ginode(inumber);
    237 		dp->di_size = sblock.lfs_fsize;
    238 		dp->di_mode = IFREG | 0600;
    239 		inodirty();
    240 	}
    241 	ndb = howmany(dp->di_size, sblock.lfs_bsize);
    242 	if (ndb < 0) {
    243 		if (debug)
    244 			printf("bad size %qu ndb %d:",
    245 			       (unsigned long long)dp->di_size, ndb);
    246 		goto unknown;
    247 	}
    248 	if (mode == IFBLK || mode == IFCHR)
    249 		ndb++;
    250 	if (mode == IFLNK) {
    251 		/*
    252 		 * Note that the old fastlink format always had di_blocks set
    253 		 * to 0.  Other than that we no longer use the `spare' field
    254 		 * (which is now the extended uid)for sanity checking, the
    255 		 * new format is the same as the old.  We simply ignore the
    256 		 * conversion altogether.  - mycroft, 19MAY1994
    257 		 */
    258 		if (doinglevel2 &&
    259 		    dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
    260 		    dp->di_blocks != 0) {
    261 			symbuf = alloca(secsize);
    262 			if (bread(fsreadfd, symbuf,
    263 				  fsbtodb(&sblock, dp->di_db[0]),
    264 				  (long)secsize) != 0)
    265 				errexit("cannot read symlink");
    266 			if (debug) {
    267 				symbuf[dp->di_size] = 0;
    268 				printf("convert symlink %d(%s)of size %qd\n",
    269 				  inumber, symbuf, (long long)dp->di_size);
    270 			}
    271 			dp = ginode(inumber);
    272 			memcpy(dp->di_shortlink, symbuf, (long)dp->di_size);
    273 			dp->di_blocks = 0;
    274 			inodirty();
    275 		}
    276 		/*
    277 		 * Fake ndb value so direct/indirect block checks below
    278 		 * will detect any garbage after symlink string.
    279 		 */
    280 		if (dp->di_size < sblock.lfs_maxsymlinklen ||
    281 		    (sblock.lfs_maxsymlinklen == 0 && dp->di_blocks == 0)) {
    282 			ndb = howmany(dp->di_size, sizeof(daddr_t));
    283 			if (ndb > NDADDR) {
    284 				j = ndb - NDADDR;
    285 				for (ndb = 1; j > 1; j--)
    286 					ndb *= NINDIR(&sblock);
    287 				ndb += NDADDR;
    288 			}
    289 		}
    290 	}
    291 	for (j = ndb; j < NDADDR; j++)
    292 		if (dp->di_db[j] != 0) {
    293 			if (debug)
    294 				printf("bad direct addr: %d\n", dp->di_db[j]);
    295 			goto unknown;
    296 		}
    297 	for (j = 0, ndb -= NDADDR; ndb > 0; j++)
    298 		ndb /= NINDIR(&sblock);
    299 	for (; j < NIADDR; j++)
    300 		if (dp->di_ib[j] != 0) {
    301 			if (debug)
    302 				printf("bad indirect addr: %d\n",
    303 				       dp->di_ib[j]);
    304 			/* goto unknown; */
    305 		}
    306 	if (ftypeok(dp) == 0)
    307 		goto unknown;
    308 	n_files++;
    309 	lncntp[inumber] = dp->di_nlink;
    310 	if (dp->di_nlink <= 0) {
    311 		zlnp = (struct zlncnt *)malloc(sizeof *zlnp);
    312 		if (zlnp == NULL) {
    313 			pfatal("LINK COUNT TABLE OVERFLOW");
    314 			if (reply("CONTINUE") == 0)
    315 				errexit("%s", "");
    316 		} else {
    317 			zlnp->zlncnt = inumber;
    318 			zlnp->next = zlnhead;
    319 			zlnhead = zlnp;
    320 		}
    321 	}
    322 	if (mode == IFDIR) {
    323 		if (dp->di_size == 0)
    324 			statemap[inumber] = DCLEAR;
    325 		else
    326 			statemap[inumber] = DSTATE;
    327 		cacheino(dp, inumber);
    328 	} else
    329 		statemap[inumber] = FSTATE;
    330 	typemap[inumber] = IFTODT(mode);
    331 #if 0				/* FFS */
    332 	if (doinglevel2 &&
    333 	    (dp->di_ouid != (u_short) - 1 || dp->di_ogid != (u_short) - 1)) {
    334 		dp = ginode(inumber);
    335 		dp->di_uid = dp->di_ouid;
    336 		dp->di_ouid = -1;
    337 		dp->di_gid = dp->di_ogid;
    338 		dp->di_ogid = -1;
    339 		inodirty();
    340 	}
    341 #endif
    342 	badblk = dupblk = 0;
    343 	idesc->id_number = inumber;
    344 	(void)ckinode(dp, idesc);
    345 	idesc->id_entryno *= btodb(sblock.lfs_fsize);
    346 	if (dp->di_blocks != idesc->id_entryno) {
    347 		pwarn("INCORRECT BLOCK COUNT I=%u (%d should be %d)",
    348 		      inumber, dp->di_blocks, idesc->id_entryno);
    349 		if (preen)
    350 			printf(" (CORRECTED)\n");
    351 		else if (reply("CORRECT") == 0)
    352 			return;
    353 		dp = ginode(inumber);
    354 		dp->di_blocks = idesc->id_entryno;
    355 		inodirty();
    356 	}
    357 	return;
    358 unknown:
    359 	pfatal("UNKNOWN FILE TYPE I=%u", inumber);
    360 	statemap[inumber] = FCLEAR;
    361 	if (reply("CLEAR") == 1) {
    362 		statemap[inumber] = USTATE;
    363 		dp = ginode(inumber);
    364 		clearinode(dp);
    365 		inodirty();
    366 	}
    367 }
    368 
    369 int
    370 pass1check(struct inodesc * idesc)
    371 {
    372 	int             res = KEEPON;
    373 	int             anyout, nfrags;
    374 	daddr_t         blkno = idesc->id_blkno;
    375 	register struct dups *dlp;
    376 	struct dups    *new;
    377 
    378 	if (!testbmap(blkno)) {
    379 		seg_table[datosn(&sblock, blkno)].su_nbytes += idesc->id_numfrags * sblock.lfs_fsize;
    380 	}
    381 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
    382 		blkerror(idesc->id_number, "BAD", blkno);
    383 		if (badblk++ >= MAXBAD) {
    384 			pwarn("EXCESSIVE BAD BLKS I=%u",
    385 			      idesc->id_number);
    386 			if (preen)
    387 				printf(" (SKIPPING)\n");
    388 			else if (reply("CONTINUE") == 0)
    389 				errexit("%s", "");
    390 			return (STOP);
    391 		}
    392 	}
    393 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
    394 		if (anyout && chkrange(blkno, 1)) {
    395 			res = SKIP;
    396 		} else if (!testbmap(blkno)) {
    397 			n_blks++;
    398 #ifndef VERBOSE_BLOCKMAP
    399 			setbmap(blkno);
    400 #else
    401 			setbmap(blkno, idesc->id_number);
    402 #endif
    403 		} else {
    404 			blkerror(idesc->id_number, "DUP", blkno);
    405 #ifdef VERBOSE_BLOCKMAP
    406 			pwarn("(lbn %d: Holder is %d)\n", idesc->id_lblkno,
    407 			      testbmap(blkno));
    408 #endif
    409 			if (dupblk++ >= MAXDUP) {
    410 				pwarn("EXCESSIVE DUP BLKS I=%u",
    411 				      idesc->id_number);
    412 				if (preen)
    413 					printf(" (SKIPPING)\n");
    414 				else if (reply("CONTINUE") == 0)
    415 					errexit("%s", "");
    416 				return (STOP);
    417 			}
    418 			new = (struct dups *)malloc(sizeof(struct dups));
    419 			if (new == NULL) {
    420 				pfatal("DUP TABLE OVERFLOW.");
    421 				if (reply("CONTINUE") == 0)
    422 					errexit("%s", "");
    423 				return (STOP);
    424 			}
    425 			new->dup = blkno;
    426 			if (muldup == 0) {
    427 				duplist = muldup = new;
    428 				new->next = 0;
    429 			} else {
    430 				new->next = muldup->next;
    431 				muldup->next = new;
    432 			}
    433 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
    434 				if (dlp->dup == blkno)
    435 					break;
    436 			if (dlp == muldup && dlp->dup != blkno)
    437 				muldup = new;
    438 		}
    439 		/*
    440 		 * count the number of blocks found in id_entryno
    441 		 */
    442 		idesc->id_entryno++;
    443 	}
    444 	return (res);
    445 }
    446