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