xref: /openbsd-src/sbin/fsck_ext2fs/pass2.c (revision 6fc3cb5247be4bc0461ed5b124198628bb3dc070)
1*6fc3cb52Smillert /*	$OpenBSD: pass2.c,v 1.16 2019/12/17 19:41:51 millert Exp $	*/
20190393fSart /*	$NetBSD: pass2.c,v 1.6 2000/01/28 16:01:46 bouyer Exp $	*/
38c424e8eSdownsj 
48c424e8eSdownsj /*
55ff4e0c8Sdownsj  * Copyright (c) 1997 Manuel Bouyer.
68c424e8eSdownsj  * Copyright (c) 1980, 1986, 1993
78c424e8eSdownsj  *	The Regents of the University of California.  All rights reserved.
88c424e8eSdownsj  *
98c424e8eSdownsj  * Redistribution and use in source and binary forms, with or without
108c424e8eSdownsj  * modification, are permitted provided that the following conditions
118c424e8eSdownsj  * are met:
128c424e8eSdownsj  * 1. Redistributions of source code must retain the above copyright
138c424e8eSdownsj  *    notice, this list of conditions and the following disclaimer.
148c424e8eSdownsj  * 2. Redistributions in binary form must reproduce the above copyright
158c424e8eSdownsj  *    notice, this list of conditions and the following disclaimer in the
168c424e8eSdownsj  *    documentation and/or other materials provided with the distribution.
171ef0d710Smillert  * 3. Neither the name of the University nor the names of its contributors
188c424e8eSdownsj  *    may be used to endorse or promote products derived from this software
198c424e8eSdownsj  *    without specific prior written permission.
208c424e8eSdownsj  *
218c424e8eSdownsj  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
228c424e8eSdownsj  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238c424e8eSdownsj  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248c424e8eSdownsj  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
258c424e8eSdownsj  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
268c424e8eSdownsj  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
278c424e8eSdownsj  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288c424e8eSdownsj  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
298c424e8eSdownsj  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
308c424e8eSdownsj  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318c424e8eSdownsj  * SUCH DAMAGE.
328c424e8eSdownsj  */
338c424e8eSdownsj 
34b9fc9a72Sderaadt #include <sys/param.h>	/* roundup */
358c424e8eSdownsj #include <sys/time.h>
368c424e8eSdownsj #include <ufs/ext2fs/ext2fs_dinode.h>
378c424e8eSdownsj #include <ufs/ext2fs/ext2fs_dir.h>
388c424e8eSdownsj #include <ufs/ext2fs/ext2fs.h>
398c424e8eSdownsj 
408c424e8eSdownsj #include <ufs/ufs/dinode.h> /* for IFMT & friends */
418c424e8eSdownsj 
428c424e8eSdownsj #include <stdio.h>
438c424e8eSdownsj #include <stdlib.h>
448c424e8eSdownsj #include <string.h>
45b9fc9a72Sderaadt #include <limits.h>
468c424e8eSdownsj 
478c424e8eSdownsj #include "fsck.h"
488c424e8eSdownsj #include "fsutil.h"
498c424e8eSdownsj #include "extern.h"
508c424e8eSdownsj 
518c424e8eSdownsj #define MINDIRSIZE	(sizeof (struct ext2fs_dirtemplate))
528c424e8eSdownsj 
53c72b5b24Smillert static int pass2check(struct inodesc *);
54c72b5b24Smillert static int blksort(const void *, const void *);
558c424e8eSdownsj 
568c424e8eSdownsj void
pass2(void)578809fabbSderaadt pass2(void)
588c424e8eSdownsj {
590190393fSart 	struct ext2fs_dinode *dp;
600190393fSart 	struct inoinfo **inpp, *inp;
618c424e8eSdownsj 	struct inoinfo **inpend;
628c424e8eSdownsj 	struct inodesc curino;
638c424e8eSdownsj 	struct ext2fs_dinode dino;
64b9fc9a72Sderaadt 	char pathbuf[PATH_MAX + 1];
658c424e8eSdownsj 
668c424e8eSdownsj 	switch (statemap[EXT2_ROOTINO]) {
678c424e8eSdownsj 
688c424e8eSdownsj 	case USTATE:
698c424e8eSdownsj 		pfatal("ROOT INODE UNALLOCATED");
708c424e8eSdownsj 		if (reply("ALLOCATE") == 0)
718c424e8eSdownsj 			errexit("%s\n", "");
728c424e8eSdownsj 		if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO)
738c424e8eSdownsj 			errexit("CANNOT ALLOCATE ROOT INODE\n");
748c424e8eSdownsj 		break;
758c424e8eSdownsj 
768c424e8eSdownsj 	case DCLEAR:
778c424e8eSdownsj 		pfatal("DUPS/BAD IN ROOT INODE");
788c424e8eSdownsj 		if (reply("REALLOCATE")) {
798c424e8eSdownsj 			freeino(EXT2_ROOTINO);
808c424e8eSdownsj 			if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO)
818c424e8eSdownsj 				errexit("CANNOT ALLOCATE ROOT INODE\n");
828c424e8eSdownsj 			break;
838c424e8eSdownsj 		}
848c424e8eSdownsj 		if (reply("CONTINUE") == 0)
858c424e8eSdownsj 			errexit("%s\n", "");
868c424e8eSdownsj 		break;
878c424e8eSdownsj 
888c424e8eSdownsj 	case FSTATE:
898c424e8eSdownsj 	case FCLEAR:
908c424e8eSdownsj 		pfatal("ROOT INODE NOT DIRECTORY");
918c424e8eSdownsj 		if (reply("REALLOCATE")) {
928c424e8eSdownsj 			freeino(EXT2_ROOTINO);
938c424e8eSdownsj 			if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO)
948c424e8eSdownsj 				errexit("CANNOT ALLOCATE ROOT INODE\n");
958c424e8eSdownsj 			break;
968c424e8eSdownsj 		}
978c424e8eSdownsj 		if (reply("FIX") == 0)
988c424e8eSdownsj 			errexit("%s\n", "");
998c424e8eSdownsj 		dp = ginode(EXT2_ROOTINO);
10060a51e06Spelikan 		dp->e2di_mode = htole16((letoh16(dp->e2di_mode) & ~IFMT) | IFDIR);
1018c424e8eSdownsj 		inodirty();
1028c424e8eSdownsj 		break;
1038c424e8eSdownsj 
1048c424e8eSdownsj 	case DSTATE:
1058c424e8eSdownsj 		break;
1068c424e8eSdownsj 
1078c424e8eSdownsj 	default:
1088c424e8eSdownsj 		errexit("BAD STATE %d FOR ROOT INODE\n", statemap[EXT2_ROOTINO]);
1098c424e8eSdownsj 	}
1108c424e8eSdownsj 
1118c424e8eSdownsj 	/*
1128c424e8eSdownsj 	 * Sort the directory list into disk block order.
1138c424e8eSdownsj 	 */
1148c424e8eSdownsj 	qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
1158c424e8eSdownsj 	/*
1168c424e8eSdownsj 	 * Check the integrity of each directory.
1178c424e8eSdownsj 	 */
1188c424e8eSdownsj 	memset(&curino, 0, sizeof(struct inodesc));
1198c424e8eSdownsj 	curino.id_type = DATA;
1208c424e8eSdownsj 	curino.id_func = pass2check;
1218c424e8eSdownsj 	inpend = &inpsort[inplast];
1228c424e8eSdownsj 	for (inpp = inpsort; inpp < inpend; inpp++) {
1238c424e8eSdownsj 		inp = *inpp;
1248c424e8eSdownsj 		if (inp->i_isize == 0)
1258c424e8eSdownsj 			continue;
1268c424e8eSdownsj 		if (inp->i_isize < MINDIRSIZE) {
1278c424e8eSdownsj 			direrror(inp->i_number, "DIRECTORY TOO SHORT");
1288c424e8eSdownsj 			inp->i_isize = roundup(MINDIRSIZE, sblock.e2fs_bsize);
1298c424e8eSdownsj 			if (reply("FIX") == 1) {
1308c424e8eSdownsj 				dp = ginode(inp->i_number);
131935730fbSniallo 				inossize(dp, inp->i_isize);
1328c424e8eSdownsj 				inodirty();
1338c424e8eSdownsj 			}
1348c424e8eSdownsj 		} else if ((inp->i_isize & (sblock.e2fs_bsize - 1)) != 0) {
135487a1948Stedu 			getpathname(pathbuf, sizeof pathbuf, inp->i_number,
136487a1948Stedu 			    inp->i_number);
13765348f21Sjasoni 			pwarn("DIRECTORY %s: LENGTH %lu NOT MULTIPLE OF %d",
13865348f21Sjasoni 			    pathbuf, (u_long)inp->i_isize, sblock.e2fs_bsize);
1398c424e8eSdownsj 			if (preen)
1408c424e8eSdownsj 				printf(" (ADJUSTED)\n");
1418c424e8eSdownsj 			inp->i_isize = roundup(inp->i_isize, sblock.e2fs_bsize);
1428c424e8eSdownsj 			if (preen || reply("ADJUST") == 1) {
1438c424e8eSdownsj 				dp = ginode(inp->i_number);
144935730fbSniallo 				inossize(dp, inp->i_isize);
1458c424e8eSdownsj 				inodirty();
1468c424e8eSdownsj 			}
1478c424e8eSdownsj 		}
148*6fc3cb52Smillert 		memset(&dino, 0, sizeof(dino));
14960a51e06Spelikan 		dino.e2di_mode = htole16(IFDIR);
150935730fbSniallo 		inossize(&dino, inp->i_isize);
1518c424e8eSdownsj 		memcpy(&dino.e2di_blocks[0], &inp->i_blks[0], (size_t)inp->i_numblks);
1528c424e8eSdownsj 		curino.id_number = inp->i_number;
1538c424e8eSdownsj 		curino.id_parent = inp->i_parent;
1548c424e8eSdownsj 		(void)ckinode(&dino, &curino);
1558c424e8eSdownsj 	}
1568c424e8eSdownsj 	/*
1578c424e8eSdownsj 	 * Now that the parents of all directories have been found,
1588c424e8eSdownsj 	 * make another pass to verify the value of `..'
1598c424e8eSdownsj 	 */
1608c424e8eSdownsj 	for (inpp = inpsort; inpp < inpend; inpp++) {
1618c424e8eSdownsj 		inp = *inpp;
1628c424e8eSdownsj 		if (inp->i_parent == 0 || inp->i_isize == 0)
1638c424e8eSdownsj 			continue;
1648c424e8eSdownsj 		if (inp->i_dotdot == inp->i_parent ||
1658c424e8eSdownsj 		    inp->i_dotdot == (ino_t)-1)
1668c424e8eSdownsj 			continue;
1678c424e8eSdownsj 		if (inp->i_dotdot == 0) {
1688c424e8eSdownsj 			inp->i_dotdot = inp->i_parent;
1698c424e8eSdownsj 			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
1708c424e8eSdownsj 			if (reply("FIX") == 0)
1718c424e8eSdownsj 				continue;
1728c424e8eSdownsj 			(void)makeentry(inp->i_number, inp->i_parent, "..");
1738c424e8eSdownsj 			lncntp[inp->i_parent]--;
1748c424e8eSdownsj 			continue;
1758c424e8eSdownsj 		}
1768c424e8eSdownsj 		fileerror(inp->i_parent, inp->i_number,
1778c424e8eSdownsj 		    "BAD INODE NUMBER FOR '..'");
1788c424e8eSdownsj 		if (reply("FIX") == 0)
1798c424e8eSdownsj 			continue;
1808c424e8eSdownsj 		lncntp[inp->i_dotdot]++;
1818c424e8eSdownsj 		lncntp[inp->i_parent]--;
1828c424e8eSdownsj 		inp->i_dotdot = inp->i_parent;
1838c424e8eSdownsj 		(void)changeino(inp->i_number, "..", inp->i_parent);
1848c424e8eSdownsj 	}
1858c424e8eSdownsj 	/*
1868c424e8eSdownsj 	 * Mark all the directories that can be found from the root.
1878c424e8eSdownsj 	 */
1888c424e8eSdownsj 	propagate();
1898c424e8eSdownsj }
1908c424e8eSdownsj 
1918c424e8eSdownsj static int
pass2check(struct inodesc * idesc)1928809fabbSderaadt pass2check(struct inodesc *idesc)
1938c424e8eSdownsj {
1940190393fSart 	struct ext2fs_direct *dirp = idesc->id_dirp;
1950190393fSart 	struct inoinfo *inp;
1968c424e8eSdownsj 	int n, entrysize, ret = 0;
1978c424e8eSdownsj 	struct ext2fs_dinode *dp;
1988c424e8eSdownsj 	char *errmsg;
1998c424e8eSdownsj 	struct ext2fs_direct proto;
200b9fc9a72Sderaadt 	char namebuf[PATH_MAX + 1];
201b9fc9a72Sderaadt 	char pathbuf[PATH_MAX + 1];
2028c424e8eSdownsj 
2038c424e8eSdownsj 	/*
2048c424e8eSdownsj 	 * check for "."
2058c424e8eSdownsj 	 */
2068c424e8eSdownsj 	if (idesc->id_entryno != 0)
2078c424e8eSdownsj 		goto chk1;
20860a51e06Spelikan 	if (letoh32(dirp->e2d_ino) != 0 && dirp->e2d_namlen == 1 &&
2098c424e8eSdownsj 		dirp->e2d_name[0] == '.') {
21060a51e06Spelikan 		if (letoh32(dirp->e2d_ino) != idesc->id_number) {
2118c424e8eSdownsj 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
21260a51e06Spelikan 			dirp->e2d_ino = htole32(idesc->id_number);
21365348f21Sjasoni 			if (reply("FIX") == 1)
21465348f21Sjasoni 				ret |= ALTERED;
21565348f21Sjasoni 		}
21665348f21Sjasoni 		if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
21765348f21Sjasoni 		    (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)
21865348f21Sjasoni 		    && (dirp->e2d_type != EXT2_FT_DIR)) {
21965348f21Sjasoni 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
22065348f21Sjasoni 			dirp->e2d_type = EXT2_FT_DIR;
2218c424e8eSdownsj 			if (reply("FIX") == 1)
2228c424e8eSdownsj 				ret |= ALTERED;
2238c424e8eSdownsj 		}
2248c424e8eSdownsj 		goto chk1;
2258c424e8eSdownsj 	}
2268c424e8eSdownsj 	direrror(idesc->id_number, "MISSING '.'");
22760a51e06Spelikan 	proto.e2d_ino = htole32(idesc->id_number);
2288c424e8eSdownsj 	proto.e2d_namlen = 1;
22965348f21Sjasoni 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
23065348f21Sjasoni 	    (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
23165348f21Sjasoni 		proto.e2d_type = EXT2_FT_DIR;
23265348f21Sjasoni 	else
23365348f21Sjasoni 		proto.e2d_type = 0;
234487a1948Stedu 	(void)strlcpy(proto.e2d_name, ".", sizeof proto.e2d_name);
2358c424e8eSdownsj 	entrysize = EXT2FS_DIRSIZ(proto.e2d_namlen);
23660a51e06Spelikan 	if (letoh32(dirp->e2d_ino) != 0 && strcmp(dirp->e2d_name, "..") != 0) {
2378c424e8eSdownsj 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
2388c424e8eSdownsj 			dirp->e2d_name);
23960a51e06Spelikan 	} else if (letoh16(dirp->e2d_reclen) < entrysize) {
2408c424e8eSdownsj 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
24160a51e06Spelikan 	} else if (letoh16(dirp->e2d_reclen) < 2 * entrysize) {
2428c424e8eSdownsj 		proto.e2d_reclen = dirp->e2d_reclen;
2438c424e8eSdownsj 		memcpy(dirp, &proto, (size_t)entrysize);
2448c424e8eSdownsj 		if (reply("FIX") == 1)
2458c424e8eSdownsj 			ret |= ALTERED;
2468c424e8eSdownsj 	} else {
24760a51e06Spelikan 		n = letoh16(dirp->e2d_reclen) - entrysize;
24860a51e06Spelikan 		proto.e2d_reclen = htole16(entrysize);
2498c424e8eSdownsj 		memcpy(dirp, &proto, (size_t)entrysize);
2508c424e8eSdownsj 		idesc->id_entryno++;
25160a51e06Spelikan 		lncntp[letoh32(dirp->e2d_ino)]--;
2528c424e8eSdownsj 		dirp = (struct ext2fs_direct *)((char *)(dirp) + entrysize);
2538c424e8eSdownsj 		memset(dirp, 0, (size_t)n);
25460a51e06Spelikan 		dirp->e2d_reclen = htole16(n);
2558c424e8eSdownsj 		if (reply("FIX") == 1)
2568c424e8eSdownsj 			ret |= ALTERED;
2578c424e8eSdownsj 	}
2588c424e8eSdownsj chk1:
2598c424e8eSdownsj 	if (idesc->id_entryno > 1)
2608c424e8eSdownsj 		goto chk2;
2618c424e8eSdownsj 	inp = getinoinfo(idesc->id_number);
26260a51e06Spelikan 	proto.e2d_ino = htole32(inp->i_parent);
2638c424e8eSdownsj 	proto.e2d_namlen = 2;
26465348f21Sjasoni 	if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
26565348f21Sjasoni 	    (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
26665348f21Sjasoni 		proto.e2d_type = EXT2_FT_DIR;
26765348f21Sjasoni 	else
26865348f21Sjasoni 		proto.e2d_type = 0;
269487a1948Stedu 	(void)strlcpy(proto.e2d_name, "..", sizeof proto.e2d_name);
27065348f21Sjasoni 	entrysize = EXT2FS_DIRSIZ(2);
2718c424e8eSdownsj 	if (idesc->id_entryno == 0) {
2728c424e8eSdownsj 		n = EXT2FS_DIRSIZ(dirp->e2d_namlen);
27360a51e06Spelikan 		if (letoh16(dirp->e2d_reclen) < n + entrysize)
2748c424e8eSdownsj 			goto chk2;
27560a51e06Spelikan 		proto.e2d_reclen = htole16(letoh16(dirp->e2d_reclen) - n);
27660a51e06Spelikan 		dirp->e2d_reclen = htole16(n);
2778c424e8eSdownsj 		idesc->id_entryno++;
27860a51e06Spelikan 		lncntp[letoh32(dirp->e2d_ino)]--;
2798c424e8eSdownsj 		dirp = (struct ext2fs_direct *)((char *)(dirp) + n);
28060a51e06Spelikan 		memset(dirp, 0, (size_t)letoh16(proto.e2d_reclen));
2818c424e8eSdownsj 		dirp->e2d_reclen = proto.e2d_reclen;
2828c424e8eSdownsj 	}
28360a51e06Spelikan 	if (letoh32(dirp->e2d_ino) != 0 &&
2848c424e8eSdownsj 	    dirp->e2d_namlen == 2 &&
28565348f21Sjasoni 	    strncmp(dirp->e2d_name, "..", 2) == 0) {
28660a51e06Spelikan 		inp->i_dotdot = letoh32(dirp->e2d_ino);
28765348f21Sjasoni 		if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
28865348f21Sjasoni 		    (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)
28965348f21Sjasoni 		    && dirp->e2d_type != EXT2_FT_DIR) {
29065348f21Sjasoni 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
29165348f21Sjasoni 			dirp->e2d_type = EXT2_FT_DIR;
29265348f21Sjasoni 			if (reply("FIX") == 1)
29365348f21Sjasoni 				ret |= ALTERED;
29465348f21Sjasoni 		}
2958c424e8eSdownsj 		goto chk2;
2968c424e8eSdownsj 	}
29760a51e06Spelikan 	if (letoh32(dirp->e2d_ino) != 0 &&
2988c424e8eSdownsj 		dirp->e2d_namlen == 1 &&
29965348f21Sjasoni 		strncmp(dirp->e2d_name, ".", 1) != 0) {
3008c424e8eSdownsj 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
3018c424e8eSdownsj 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
3028c424e8eSdownsj 			dirp->e2d_name);
3038c424e8eSdownsj 		inp->i_dotdot = (ino_t)-1;
30460a51e06Spelikan 	} else if (letoh16(dirp->e2d_reclen) < entrysize) {
3058c424e8eSdownsj 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
3068c424e8eSdownsj 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
3078c424e8eSdownsj 		inp->i_dotdot = (ino_t)-1;
3088c424e8eSdownsj 	} else if (inp->i_parent != 0) {
3098c424e8eSdownsj 		/*
3108c424e8eSdownsj 		 * We know the parent, so fix now.
3118c424e8eSdownsj 		 */
3128c424e8eSdownsj 		inp->i_dotdot = inp->i_parent;
3138c424e8eSdownsj 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
3148c424e8eSdownsj 		proto.e2d_reclen = dirp->e2d_reclen;
3158c424e8eSdownsj 		memcpy(dirp, &proto, (size_t)entrysize);
3168c424e8eSdownsj 		if (reply("FIX") == 1)
3178c424e8eSdownsj 			ret |= ALTERED;
3188c424e8eSdownsj 	}
3198c424e8eSdownsj 	idesc->id_entryno++;
32060a51e06Spelikan 	if (letoh32(dirp->e2d_ino) != 0)
32160a51e06Spelikan 		lncntp[letoh32(dirp->e2d_ino)]--;
3228c424e8eSdownsj 	return (ret|KEEPON);
3238c424e8eSdownsj chk2:
32460a51e06Spelikan 	if (letoh32(dirp->e2d_ino) == 0)
3258c424e8eSdownsj 		return (ret|KEEPON);
3268c424e8eSdownsj 	if (dirp->e2d_namlen <= 2 &&
3278c424e8eSdownsj 	    dirp->e2d_name[0] == '.' &&
3288c424e8eSdownsj 	    idesc->id_entryno >= 2) {
3298c424e8eSdownsj 		if (dirp->e2d_namlen == 1) {
3308c424e8eSdownsj 			direrror(idesc->id_number, "EXTRA '.' ENTRY");
3318c424e8eSdownsj 			dirp->e2d_ino = 0;
3328c424e8eSdownsj 			if (reply("FIX") == 1)
3338c424e8eSdownsj 				ret |= ALTERED;
3348c424e8eSdownsj 			return (KEEPON | ret);
3358c424e8eSdownsj 		}
3368c424e8eSdownsj 		if (dirp->e2d_name[1] == '.') {
3378c424e8eSdownsj 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
3388c424e8eSdownsj 			dirp->e2d_ino = 0;
3398c424e8eSdownsj 			if (reply("FIX") == 1)
3408c424e8eSdownsj 				ret |= ALTERED;
3418c424e8eSdownsj 			return (KEEPON | ret);
3428c424e8eSdownsj 		}
3438c424e8eSdownsj 	}
3448c424e8eSdownsj 	idesc->id_entryno++;
3458c424e8eSdownsj 	n = 0;
34660a51e06Spelikan 	if (letoh32(dirp->e2d_ino) > maxino ||
34760a51e06Spelikan 		(letoh32(dirp->e2d_ino) < EXT2_FIRSTINO &&
34860a51e06Spelikan 		 letoh32(dirp->e2d_ino) != EXT2_ROOTINO)) {
34960a51e06Spelikan 		fileerror(idesc->id_number, letoh32(dirp->e2d_ino), "I OUT OF RANGE");
3508c424e8eSdownsj 		n = reply("REMOVE");
3518c424e8eSdownsj 	} else {
3528c424e8eSdownsj again:
35360a51e06Spelikan 		switch (statemap[letoh32(dirp->e2d_ino)]) {
3548c424e8eSdownsj 		case USTATE:
3558c424e8eSdownsj 			if (idesc->id_entryno <= 2)
3568c424e8eSdownsj 				break;
35760a51e06Spelikan 			fileerror(idesc->id_number, letoh32(dirp->e2d_ino), "UNALLOCATED");
3588c424e8eSdownsj 			n = reply("REMOVE");
3598c424e8eSdownsj 			break;
3608c424e8eSdownsj 
3618c424e8eSdownsj 		case DCLEAR:
3628c424e8eSdownsj 		case FCLEAR:
3638c424e8eSdownsj 			if (idesc->id_entryno <= 2)
3648c424e8eSdownsj 				break;
36560a51e06Spelikan 			if (statemap[letoh32(dirp->e2d_ino)] == FCLEAR)
3668c424e8eSdownsj 				errmsg = "DUP/BAD";
3678c424e8eSdownsj 			else if (!preen)
3688c424e8eSdownsj 				errmsg = "ZERO LENGTH DIRECTORY";
3698c424e8eSdownsj 			else {
3708c424e8eSdownsj 				n = 1;
3718c424e8eSdownsj 				break;
3728c424e8eSdownsj 			}
37360a51e06Spelikan 			fileerror(idesc->id_number, letoh32(dirp->e2d_ino), errmsg);
3748c424e8eSdownsj 			if ((n = reply("REMOVE")) == 1)
3758c424e8eSdownsj 				break;
37660a51e06Spelikan 			dp = ginode(letoh32(dirp->e2d_ino));
37760a51e06Spelikan 			statemap[letoh32(dirp->e2d_ino)] =
37860a51e06Spelikan 			    (letoh16(dp->e2di_mode) & IFMT) == IFDIR ? DSTATE : FSTATE;
37960a51e06Spelikan 			lncntp[letoh32(dirp->e2d_ino)] = letoh16(dp->e2di_nlink);
3808c424e8eSdownsj 			goto again;
3818c424e8eSdownsj 
3828c424e8eSdownsj 		case DSTATE:
3838c424e8eSdownsj 		case DFOUND:
38460a51e06Spelikan 			inp = getinoinfo(letoh32(dirp->e2d_ino));
3858c424e8eSdownsj 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
386487a1948Stedu 				getpathname(pathbuf, sizeof pathbuf,
387487a1948Stedu 				    idesc->id_number, idesc->id_number);
388487a1948Stedu 				getpathname(namebuf, sizeof namebuf,
38960a51e06Spelikan 				    letoh32(dirp->e2d_ino), letoh32(dirp->e2d_ino));
3908c424e8eSdownsj 				pwarn("%s %s %s\n", pathbuf,
3918c424e8eSdownsj 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
3928c424e8eSdownsj 				    namebuf);
3938c424e8eSdownsj 				if (preen)
3948c424e8eSdownsj 					printf(" (IGNORED)\n");
3958c424e8eSdownsj 				else if ((n = reply("REMOVE")) == 1)
3968c424e8eSdownsj 					break;
3978c424e8eSdownsj 			}
3988c424e8eSdownsj 			if (idesc->id_entryno > 2)
3998c424e8eSdownsj 				inp->i_parent = idesc->id_number;
4008c424e8eSdownsj 			/* fall through */
4018c424e8eSdownsj 
4028c424e8eSdownsj 		case FSTATE:
40365348f21Sjasoni 			if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
40465348f21Sjasoni 			    (sblock.e2fs.e2fs_features_incompat &
40565348f21Sjasoni 				EXT2F_INCOMPAT_FTYPE) &&
40665348f21Sjasoni 			    dirp->e2d_type !=
40760a51e06Spelikan 				inot2ext2dt(typemap[letoh32(dirp->e2d_ino)])) {
40865348f21Sjasoni 				dirp->e2d_type =
40960a51e06Spelikan 				    inot2ext2dt(typemap[letoh32(dirp->e2d_ino)]);
41065348f21Sjasoni 				fileerror(idesc->id_number,
41160a51e06Spelikan 				    letoh32(dirp->e2d_ino),
41265348f21Sjasoni 				    "BAD TYPE VALUE");
41365348f21Sjasoni 				if (reply("FIX") == 1)
41465348f21Sjasoni 					ret |= ALTERED;
41565348f21Sjasoni 			}
41660a51e06Spelikan 			lncntp[letoh32(dirp->e2d_ino)]--;
4178c424e8eSdownsj 			break;
4188c424e8eSdownsj 
4198c424e8eSdownsj 		default:
4203b92bd08Sderaadt 			errexit("BAD STATE %d FOR INODE I=%llu\n",
42160a51e06Spelikan 			    statemap[letoh32(dirp->e2d_ino)],
42260a51e06Spelikan 			    (unsigned long long)letoh32(dirp->e2d_ino));
4238c424e8eSdownsj 		}
4248c424e8eSdownsj 	}
4258c424e8eSdownsj 	if (n == 0)
4268c424e8eSdownsj 		return (ret|KEEPON);
4278c424e8eSdownsj 	dirp->e2d_ino = 0;
4288c424e8eSdownsj 	return (ret|KEEPON|ALTERED);
4298c424e8eSdownsj }
4308c424e8eSdownsj 
4318c424e8eSdownsj /*
4328c424e8eSdownsj  * Routine to sort disk blocks.
4338c424e8eSdownsj  */
4348c424e8eSdownsj static int
blksort(const void * inpp1,const void * inpp2)4358809fabbSderaadt blksort(const void *inpp1, const void *inpp2)
4368c424e8eSdownsj {
4378c424e8eSdownsj 	return ((* (struct inoinfo **) inpp1)->i_blks[0] -
4388c424e8eSdownsj 		(* (struct inoinfo **) inpp2)->i_blks[0]);
4398c424e8eSdownsj }
440