xref: /netbsd-src/sbin/fsck_ext2fs/pass1.c (revision 01853927002e5a973def52a1d2db5e4608998a86)
1*01853927Schristos /*	$NetBSD: pass1.c,v 1.26 2017/04/21 19:33:56 christos Exp $	*/
2bf07c871Sagc 
3bf07c871Sagc /*
4bf07c871Sagc  * Copyright (c) 1980, 1986, 1993
5bf07c871Sagc  *	The Regents of the University of California.  All rights reserved.
6bf07c871Sagc  *
7bf07c871Sagc  * Redistribution and use in source and binary forms, with or without
8bf07c871Sagc  * modification, are permitted provided that the following conditions
9bf07c871Sagc  * are met:
10bf07c871Sagc  * 1. Redistributions of source code must retain the above copyright
11bf07c871Sagc  *    notice, this list of conditions and the following disclaimer.
12bf07c871Sagc  * 2. Redistributions in binary form must reproduce the above copyright
13bf07c871Sagc  *    notice, this list of conditions and the following disclaimer in the
14bf07c871Sagc  *    documentation and/or other materials provided with the distribution.
15bf07c871Sagc  * 3. Neither the name of the University nor the names of its contributors
16bf07c871Sagc  *    may be used to endorse or promote products derived from this software
17bf07c871Sagc  *    without specific prior written permission.
18bf07c871Sagc  *
19bf07c871Sagc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20bf07c871Sagc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21bf07c871Sagc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22bf07c871Sagc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23bf07c871Sagc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24bf07c871Sagc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25bf07c871Sagc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26bf07c871Sagc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27bf07c871Sagc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28bf07c871Sagc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29bf07c871Sagc  * SUCH DAMAGE.
30bf07c871Sagc  */
318f7c2b37Sbouyer 
328f7c2b37Sbouyer /*
338f7c2b37Sbouyer  * Copyright (c) 1997 Manuel Bouyer.
348f7c2b37Sbouyer  *
358f7c2b37Sbouyer  * Redistribution and use in source and binary forms, with or without
368f7c2b37Sbouyer  * modification, are permitted provided that the following conditions
378f7c2b37Sbouyer  * are met:
388f7c2b37Sbouyer  * 1. Redistributions of source code must retain the above copyright
398f7c2b37Sbouyer  *    notice, this list of conditions and the following disclaimer.
408f7c2b37Sbouyer  * 2. Redistributions in binary form must reproduce the above copyright
418f7c2b37Sbouyer  *    notice, this list of conditions and the following disclaimer in the
428f7c2b37Sbouyer  *    documentation and/or other materials provided with the distribution.
438f7c2b37Sbouyer  *
442f853da9Sbouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
452f853da9Sbouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
462f853da9Sbouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
472f853da9Sbouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
482f853da9Sbouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
492f853da9Sbouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
502f853da9Sbouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
512f853da9Sbouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
522f853da9Sbouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
532f853da9Sbouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
548f7c2b37Sbouyer  */
558f7c2b37Sbouyer 
564b836889Slukem #include <sys/cdefs.h>
578f7c2b37Sbouyer #ifndef lint
588f7c2b37Sbouyer #if 0
598f7c2b37Sbouyer static char sccsid[] = "@(#)pass1.c	8.1 (Berkeley) 6/5/93";
608f7c2b37Sbouyer #else
61*01853927Schristos __RCSID("$NetBSD: pass1.c,v 1.26 2017/04/21 19:33:56 christos Exp $");
628f7c2b37Sbouyer #endif
638f7c2b37Sbouyer #endif /* not lint */
648f7c2b37Sbouyer 
658f7c2b37Sbouyer #include <sys/param.h>
668f7c2b37Sbouyer #include <sys/time.h>
678f7c2b37Sbouyer #include <ufs/ext2fs/ext2fs_dinode.h>
688f7c2b37Sbouyer #include <ufs/ext2fs/ext2fs_dir.h>
698f7c2b37Sbouyer #include <ufs/ext2fs/ext2fs.h>
708f7c2b37Sbouyer 
718f7c2b37Sbouyer #include <ufs/ufs/dinode.h> /* for IFMT & friends */
728f7c2b37Sbouyer 
738f7c2b37Sbouyer #include <stdio.h>
748f7c2b37Sbouyer #include <stdlib.h>
758f7c2b37Sbouyer #include <string.h>
768bb2a03bSkleink #include <time.h>
778f7c2b37Sbouyer 
788f7c2b37Sbouyer #include "fsck.h"
798f7c2b37Sbouyer #include "extern.h"
808f7c2b37Sbouyer #include "fsutil.h"
81481ad7b0Slukem #include "exitvalues.h"
828f7c2b37Sbouyer 
838f7c2b37Sbouyer static daddr_t badblk;
848f7c2b37Sbouyer static daddr_t dupblk;
85ccde05f0Sxtraeme static void checkinode(ino_t, struct inodesc *);
868f7c2b37Sbouyer 
878f7c2b37Sbouyer void
pass1(void)88ccde05f0Sxtraeme pass1(void)
898f7c2b37Sbouyer {
908f7c2b37Sbouyer 	ino_t inumber;
9109d4663fSbouyer 	int c, i;
925bd52bbaSlukem 	size_t j;
9309d4663fSbouyer 	daddr_t dbase;
948f7c2b37Sbouyer 	struct inodesc idesc;
958f7c2b37Sbouyer 
968f7c2b37Sbouyer 	/*
978f7c2b37Sbouyer 	 * Set file system reserved blocks in used block map.
988f7c2b37Sbouyer 	 */
998f7c2b37Sbouyer 	for (c = 0; c < sblock.e2fs_ncg; c++) {
10009d4663fSbouyer 		dbase = c * sblock.e2fs.e2fs_bpg +
10109d4663fSbouyer 		    sblock.e2fs.e2fs_first_dblock;
10209d4663fSbouyer 		/* Mark the blocks used for the inode table */
1035db82feaSbouyer 		if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables) >= dbase) {
10409d4663fSbouyer 			for (i = 0; i < sblock.e2fs_itpg; i++)
1055db82feaSbouyer 				setbmap(
1065db82feaSbouyer 				    fs2h32(sblock.e2fs_gd[c].ext2bgd_i_tables)
1075db82feaSbouyer 				    + i);
10809d4663fSbouyer 		}
10909d4663fSbouyer 		/* Mark the blocks used for the block bitmap */
1105db82feaSbouyer 		if (fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap) >= dbase)
1115db82feaSbouyer 			setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_b_bitmap));
11209d4663fSbouyer 		/* Mark the blocks used for the inode bitmap */
1135db82feaSbouyer 		if (fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap) >= dbase)
1145db82feaSbouyer 			setbmap(fs2h32(sblock.e2fs_gd[c].ext2bgd_i_bitmap));
1158f7c2b37Sbouyer 
11609d4663fSbouyer 		if (sblock.e2fs.e2fs_rev == E2FS_REV0 ||
11709d4663fSbouyer 		    (sblock.e2fs.e2fs_features_rocompat &
11809d4663fSbouyer 			EXT2F_ROCOMPAT_SPARSESUPER) == 0 ||
11909d4663fSbouyer 		    cg_has_sb(c)) {
12009d4663fSbouyer 			/* Mark copuy of SB and descriptors */
12109d4663fSbouyer 			setbmap(dbase);
12209d4663fSbouyer 			for (i = 1; i <= sblock.e2fs_ngdb; i++)
12309d4663fSbouyer 				setbmap(dbase+i);
12409d4663fSbouyer 		}
12509d4663fSbouyer 
12609d4663fSbouyer 
12709d4663fSbouyer 		if (c == 0) {
12809d4663fSbouyer 			for(i = 0; i < dbase; i++)
1298f7c2b37Sbouyer 				setbmap(i);
1308f7c2b37Sbouyer 		}
13109d4663fSbouyer 	}
1328f7c2b37Sbouyer 
1338f7c2b37Sbouyer 	/*
1348f7c2b37Sbouyer 	 * Find all allocated blocks.
1358f7c2b37Sbouyer 	 */
1368f7c2b37Sbouyer 	memset(&idesc, 0, sizeof(struct inodesc));
1378f7c2b37Sbouyer 	idesc.id_type = ADDR;
1388f7c2b37Sbouyer 	idesc.id_func = pass1check;
1398f7c2b37Sbouyer 	inumber = 1;
1408f7c2b37Sbouyer 	n_files = n_blks = 0;
1418f7c2b37Sbouyer 	resetinodebuf();
1428f7c2b37Sbouyer 	for (c = 0; c < sblock.e2fs_ncg; c++) {
1435bd52bbaSlukem 		for (j = 0;
1445bd52bbaSlukem 			j < sblock.e2fs.e2fs_ipg && inumber <= sblock.e2fs.e2fs_icount;
1455bd52bbaSlukem 			j++, inumber++) {
1468f7c2b37Sbouyer 			if (inumber < EXT2_ROOTINO) /* XXX */
1478f7c2b37Sbouyer 				continue;
1488f7c2b37Sbouyer 			checkinode(inumber, &idesc);
1498f7c2b37Sbouyer 		}
1508f7c2b37Sbouyer 	}
1518f7c2b37Sbouyer 	freeinodebuf();
1528f7c2b37Sbouyer }
1538f7c2b37Sbouyer 
1548f7c2b37Sbouyer static void
checkinode(ino_t inumber,struct inodesc * idesc)155ccde05f0Sxtraeme checkinode(ino_t inumber, struct inodesc *idesc)
1568f7c2b37Sbouyer {
1574b836889Slukem 	struct ext2fs_dinode *dp;
1588f7c2b37Sbouyer 	struct zlncnt *zlnp;
1598f7c2b37Sbouyer 	int ndb, j;
1608f7c2b37Sbouyer 	mode_t mode;
1618f7c2b37Sbouyer 
1628f7c2b37Sbouyer 	dp = getnextinode(inumber);
1631e795b03Stsutsui 	if (inumber < EXT2_FIRSTINO &&
1641e795b03Stsutsui 	    inumber != EXT2_ROOTINO &&
1651e795b03Stsutsui 	    !(inumber == EXT2_RESIZEINO &&
1661e795b03Stsutsui 	      (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0))
1678f7c2b37Sbouyer 		return;
1688f7c2b37Sbouyer 
1697052d78bSbouyer 	mode = fs2h16(dp->e2di_mode) & IFMT;
1708f7c2b37Sbouyer 	if (mode == 0 || (dp->e2di_dtime != 0 && dp->e2di_nlink == 0)) {
1718f7c2b37Sbouyer 		if (mode == 0 && (
1728f7c2b37Sbouyer 		    memcmp(dp->e2di_blocks, zino.e2di_blocks,
173dcd34a91Sdholland 		    (EXT2FS_NDADDR + EXT2FS_NIADDR) * sizeof(u_int32_t)) ||
174d53c382dSws 		    dp->e2di_mode || inosize(dp))) {
175c4ee9f6dSchristos 			pfatal("PARTIALLY ALLOCATED INODE I=%llu",
176c4ee9f6dSchristos 			    (unsigned long long)inumber);
1778f7c2b37Sbouyer 			if (reply("CLEAR") == 1) {
1788f7c2b37Sbouyer 				dp = ginode(inumber);
1798f7c2b37Sbouyer 				clearinode(dp);
1808f7c2b37Sbouyer 				inodirty();
1818f7c2b37Sbouyer 			}
1828f7c2b37Sbouyer 		}
1838f7c2b37Sbouyer #ifdef notyet /* it seems that dtime == 0 is valid for a unallocated inode */
1848f7c2b37Sbouyer 		if (dp->e2di_dtime == 0) {
185c4ee9f6dSchristos 			pwarn("DELETED INODE I=%llu HAS A NULL DTIME",
186c4ee9f6dSchristos 			    (unsigned long long)inumber);
1878f7c2b37Sbouyer 			if (preen) {
1888f7c2b37Sbouyer 				printf(" (CORRECTED)\n");
1898f7c2b37Sbouyer 			}
1908f7c2b37Sbouyer 			if (preen || reply("CORRECT")) {
1918f7c2b37Sbouyer 				time_t t;
1928f7c2b37Sbouyer 				time(&t);
1937052d78bSbouyer 				dp->e2di_dtime = h2fs32(t);
1948f7c2b37Sbouyer 				dp = ginode(inumber);
1958f7c2b37Sbouyer 				inodirty();
1968f7c2b37Sbouyer 			}
1978f7c2b37Sbouyer 		}
1988f7c2b37Sbouyer #endif
1998f7c2b37Sbouyer 		statemap[inumber] = USTATE;
2008f7c2b37Sbouyer 		return;
2018f7c2b37Sbouyer 	}
2028f7c2b37Sbouyer 	lastino = inumber;
2038f7c2b37Sbouyer 	if (dp->e2di_dtime != 0) {
204c68c36a5Schristos 		pwarn("INODE I=%llu HAS DTIME=%s",
205c68c36a5Schristos 		    (unsigned long long)inumber,
206c68c36a5Schristos 		    print_mtime(fs2h32(dp->e2di_dtime)));
2078f7c2b37Sbouyer 		if (preen) {
2088f7c2b37Sbouyer 			printf(" (CORRECTED)\n");
2098f7c2b37Sbouyer 		}
2108f7c2b37Sbouyer 		if (preen || reply("CORRECT")) {
2118f7c2b37Sbouyer 			dp = ginode(inumber);
2128f7c2b37Sbouyer 			dp->e2di_dtime = 0;
2138f7c2b37Sbouyer 			inodirty();
2148f7c2b37Sbouyer 		}
2158f7c2b37Sbouyer 	}
216d53c382dSws 	if (inosize(dp) + sblock.e2fs_bsize - 1 < inosize(dp)) {
2178f7c2b37Sbouyer 		if (debug)
218d53c382dSws 			printf("bad size %llu:", (unsigned long long)inosize(dp));
2198f7c2b37Sbouyer 		goto unknown;
2208f7c2b37Sbouyer 	}
2218f7c2b37Sbouyer 	if (!preen && mode == IFMT && reply("HOLD BAD BLOCK") == 1) {
2228f7c2b37Sbouyer 		dp = ginode(inumber);
2237052d78bSbouyer 		dp->e2di_mode = h2fs16(IFREG|0600);
224d53c382dSws 		inossize(dp, sblock.e2fs_bsize);
2258f7c2b37Sbouyer 		inodirty();
2268f7c2b37Sbouyer 	}
227d53c382dSws 	ndb = howmany(inosize(dp), sblock.e2fs_bsize);
2288f7c2b37Sbouyer 	if (ndb < 0) {
2298f7c2b37Sbouyer 		if (debug)
230d53c382dSws 			printf("bad size %llu ndb %d:",
231d53c382dSws 			    (unsigned long long)inosize(dp), ndb);
2328f7c2b37Sbouyer 		goto unknown;
2338f7c2b37Sbouyer 	}
2348f7c2b37Sbouyer 	if (mode == IFBLK || mode == IFCHR)
2358f7c2b37Sbouyer 		ndb++;
2368f7c2b37Sbouyer 	if (mode == IFLNK) {
2378f7c2b37Sbouyer 		/*
2388f7c2b37Sbouyer 		 * Fake ndb value so direct/indirect block checks below
2398f7c2b37Sbouyer 		 * will detect any garbage after symlink string.
2408f7c2b37Sbouyer 		 */
241d53c382dSws 		if (inosize(dp) < EXT2_MAXSYMLINKLEN ||
242*01853927Schristos 		    (EXT2_MAXSYMLINKLEN == 0 && inonblock(dp) == 0)) {
243d53c382dSws 			ndb = howmany(inosize(dp), sizeof(u_int32_t));
244dcd34a91Sdholland 			if (ndb > EXT2FS_NDADDR) {
245dcd34a91Sdholland 				j = ndb - EXT2FS_NDADDR;
2468f7c2b37Sbouyer 				for (ndb = 1; j > 1; j--)
247f1333577Sdholland 					ndb *= EXT2_NINDIR(&sblock);
248dcd34a91Sdholland 				ndb += EXT2FS_NDADDR;
2498f7c2b37Sbouyer 			}
2508f7c2b37Sbouyer 		}
2518f7c2b37Sbouyer 	}
2521bb2b4ddSbouyer 	/* Linux puts things in blocks for FIFO, so skip this check */
2531bb2b4ddSbouyer 	if (mode != IFIFO) {
254dcd34a91Sdholland 		for (j = ndb; j < EXT2FS_NDADDR; j++)
2558f7c2b37Sbouyer 			if (dp->e2di_blocks[j] != 0) {
2568f7c2b37Sbouyer 				if (debug)
2571bb2b4ddSbouyer 					printf("bad direct addr: %d\n",
2581bb2b4ddSbouyer 					    fs2h32(dp->e2di_blocks[j]));
2598f7c2b37Sbouyer 				goto unknown;
2608f7c2b37Sbouyer 			}
261dcd34a91Sdholland 		for (j = 0, ndb -= EXT2FS_NDADDR; ndb > 0; j++)
262f1333577Sdholland 			ndb /= EXT2_NINDIR(&sblock);
263dcd34a91Sdholland 		for (; j < EXT2FS_NIADDR; j++) {
264dcd34a91Sdholland 			if (dp->e2di_blocks[j+EXT2FS_NDADDR] != 0) {
2658f7c2b37Sbouyer 				if (debug)
2668f7c2b37Sbouyer 					printf("bad indirect addr: %d\n",
267dcd34a91Sdholland 					    fs2h32(dp->e2di_blocks[j+EXT2FS_NDADDR]));
2688f7c2b37Sbouyer 				goto unknown;
2698f7c2b37Sbouyer 			}
2701bb2b4ddSbouyer 		}
2711bb2b4ddSbouyer 	}
2728f7c2b37Sbouyer 	if (ftypeok(dp) == 0)
2738f7c2b37Sbouyer 		goto unknown;
2741e795b03Stsutsui 	if (inumber >= EXT2_FIRSTINO || inumber == EXT2_ROOTINO) {
2751e795b03Stsutsui 		/* Don't count reserved inodes except root */
2768f7c2b37Sbouyer 		n_files++;
2771e795b03Stsutsui 	}
2787052d78bSbouyer 	lncntp[inumber] = fs2h16(dp->e2di_nlink);
2797052d78bSbouyer 	if (dp->e2di_nlink == 0) {
2802af45ff9Stsutsui 		zlnp = malloc(sizeof *zlnp);
2818f7c2b37Sbouyer 		if (zlnp == NULL) {
2828f7c2b37Sbouyer 			pfatal("LINK COUNT TABLE OVERFLOW");
2838f7c2b37Sbouyer 			if (reply("CONTINUE") == 0)
284481ad7b0Slukem 				exit(FSCK_EXIT_CHECK_FAILED);
2858f7c2b37Sbouyer 		} else {
2868f7c2b37Sbouyer 			zlnp->zlncnt = inumber;
2878f7c2b37Sbouyer 			zlnp->next = zlnhead;
2888f7c2b37Sbouyer 			zlnhead = zlnp;
2898f7c2b37Sbouyer 		}
2908f7c2b37Sbouyer 	}
2918f7c2b37Sbouyer 	if (mode == IFDIR) {
292d53c382dSws 		if (inosize(dp) == 0)
2938f7c2b37Sbouyer 			statemap[inumber] = DCLEAR;
2948f7c2b37Sbouyer 		else
2958f7c2b37Sbouyer 			statemap[inumber] = DSTATE;
2968f7c2b37Sbouyer 		cacheino(dp, inumber);
2978f7c2b37Sbouyer 	} else {
2988f7c2b37Sbouyer 		statemap[inumber] = FSTATE;
2998f7c2b37Sbouyer 	}
30009d4663fSbouyer 	typemap[inumber] = E2IFTODT(mode);
3018f7c2b37Sbouyer 	badblk = dupblk = 0;
3028f7c2b37Sbouyer 	idesc->id_number = inumber;
3038f7c2b37Sbouyer 	(void)ckinode(dp, idesc);
3048f7c2b37Sbouyer 	idesc->id_entryno *= btodb(sblock.e2fs_bsize);
305a3904b0bSjakllsch 	if (inonblock(dp) != (uint32_t)idesc->id_entryno) {
306a3904b0bSjakllsch 		pwarn("INCORRECT BLOCK COUNT I=%llu (%llu should be %d)",
307a3904b0bSjakllsch 		    (unsigned long long)inumber,
308a3904b0bSjakllsch 		    (unsigned long long)inonblock(dp),
309c4ee9f6dSchristos 		    idesc->id_entryno);
3108f7c2b37Sbouyer 		if (preen)
3118f7c2b37Sbouyer 			printf(" (CORRECTED)\n");
3128f7c2b37Sbouyer 		else if (reply("CORRECT") == 0)
3138f7c2b37Sbouyer 			return;
3148f7c2b37Sbouyer 		dp = ginode(inumber);
315a3904b0bSjakllsch 		inosnblock(dp, idesc->id_entryno);
3168f7c2b37Sbouyer 		inodirty();
3178f7c2b37Sbouyer 	}
3188f7c2b37Sbouyer 	return;
3198f7c2b37Sbouyer unknown:
320c4ee9f6dSchristos 	pfatal("UNKNOWN FILE TYPE I=%llu", (unsigned long long)inumber);
3218f7c2b37Sbouyer 	statemap[inumber] = FCLEAR;
3228f7c2b37Sbouyer 	if (reply("CLEAR") == 1) {
3238f7c2b37Sbouyer 		statemap[inumber] = USTATE;
3248f7c2b37Sbouyer 		dp = ginode(inumber);
3258f7c2b37Sbouyer 		clearinode(dp);
3268f7c2b37Sbouyer 		inodirty();
3278f7c2b37Sbouyer 	}
3288f7c2b37Sbouyer }
3298f7c2b37Sbouyer 
3308f7c2b37Sbouyer int
pass1check(struct inodesc * idesc)331ccde05f0Sxtraeme pass1check(struct inodesc *idesc)
3328f7c2b37Sbouyer {
3338f7c2b37Sbouyer 	int res = KEEPON;
3348f7c2b37Sbouyer 	int anyout, nfrags;
3358f7c2b37Sbouyer 	daddr_t blkno = idesc->id_blkno;
3364b836889Slukem 	struct dups *dlp;
3378f7c2b37Sbouyer 	struct dups *new;
3388f7c2b37Sbouyer 
3398f7c2b37Sbouyer 	if ((anyout = chkrange(blkno, idesc->id_numfrags)) != 0) {
3408f7c2b37Sbouyer 		blkerror(idesc->id_number, "BAD", blkno);
3418f7c2b37Sbouyer 		if (badblk++ >= MAXBAD) {
342c4ee9f6dSchristos 			pwarn("EXCESSIVE BAD BLKS I=%llu",
343c4ee9f6dSchristos 			    (unsigned long long)idesc->id_number);
3448f7c2b37Sbouyer 			if (preen)
3458f7c2b37Sbouyer 				printf(" (SKIPPING)\n");
3468f7c2b37Sbouyer 			else if (reply("CONTINUE") == 0)
347481ad7b0Slukem 				exit(FSCK_EXIT_CHECK_FAILED);
3488f7c2b37Sbouyer 			return (STOP);
3498f7c2b37Sbouyer 		}
3508f7c2b37Sbouyer 	}
3518f7c2b37Sbouyer 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
3528f7c2b37Sbouyer 		if (anyout && chkrange(blkno, 1)) {
3538f7c2b37Sbouyer 			res = SKIP;
3548f7c2b37Sbouyer 		} else if (!testbmap(blkno)) {
3558f7c2b37Sbouyer 			n_blks++;
3568f7c2b37Sbouyer 			setbmap(blkno);
3578f7c2b37Sbouyer 		} else {
3588f7c2b37Sbouyer 			blkerror(idesc->id_number, "DUP", blkno);
3598f7c2b37Sbouyer 			if (dupblk++ >= MAXDUP) {
360c4ee9f6dSchristos 				pwarn("EXCESSIVE DUP BLKS I=%llu",
361c4ee9f6dSchristos 				    (unsigned long long)idesc->id_number);
3628f7c2b37Sbouyer 				if (preen)
3638f7c2b37Sbouyer 					printf(" (SKIPPING)\n");
3648f7c2b37Sbouyer 				else if (reply("CONTINUE") == 0)
365481ad7b0Slukem 					exit(FSCK_EXIT_CHECK_FAILED);
3668f7c2b37Sbouyer 				return (STOP);
3678f7c2b37Sbouyer 			}
3682af45ff9Stsutsui 			new = malloc(sizeof(struct dups));
3698f7c2b37Sbouyer 			if (new == NULL) {
3708f7c2b37Sbouyer 				pfatal("DUP TABLE OVERFLOW.");
3718f7c2b37Sbouyer 				if (reply("CONTINUE") == 0)
372481ad7b0Slukem 					exit(FSCK_EXIT_CHECK_FAILED);
3738f7c2b37Sbouyer 				return (STOP);
3748f7c2b37Sbouyer 			}
3758f7c2b37Sbouyer 			new->dup = blkno;
3768f7c2b37Sbouyer 			if (muldup == 0) {
3778f7c2b37Sbouyer 				duplist = muldup = new;
3788f7c2b37Sbouyer 				new->next = 0;
3798f7c2b37Sbouyer 			} else {
3808f7c2b37Sbouyer 				new->next = muldup->next;
3818f7c2b37Sbouyer 				muldup->next = new;
3828f7c2b37Sbouyer 			}
3838f7c2b37Sbouyer 			for (dlp = duplist; dlp != muldup; dlp = dlp->next)
3848f7c2b37Sbouyer 				if (dlp->dup == blkno)
3858f7c2b37Sbouyer 					break;
3868f7c2b37Sbouyer 			if (dlp == muldup && dlp->dup != blkno)
3878f7c2b37Sbouyer 				muldup = new;
3888f7c2b37Sbouyer 		}
3898f7c2b37Sbouyer 		/*
3908f7c2b37Sbouyer 		 * count the number of blocks found in id_entryno
3918f7c2b37Sbouyer 		 */
3928f7c2b37Sbouyer 		idesc->id_entryno++;
3938f7c2b37Sbouyer 	}
3948f7c2b37Sbouyer 	return (res);
3958f7c2b37Sbouyer }
396