pass2.c revision 102411
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1980, 1986, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
34102411Scharnier#if 0
351558Srgrimes#ifndef lint
3623675Speterstatic const char sccsid[] = "@(#)pass2.c	8.9 (Berkeley) 4/28/95";
37102411Scharnier#endif /* not lint */
3841477Sjulian#endif
391558Srgrimes
40102411Scharnier#include <sys/cdefs.h>
41102411Scharnier__FBSDID("$FreeBSD: head/sbin/fsck_ffs/pass2.c 102411 2002-08-25 13:10:45Z charnier $");
42102411Scharnier
431558Srgrimes#include <sys/param.h>
4423675Speter
451558Srgrimes#include <ufs/ufs/dinode.h>
461558Srgrimes#include <ufs/ufs/dir.h>
4774556Smckusick#include <ufs/ffs/fs.h>
4823799Sbde
4923675Speter#include <err.h>
50101037Smux#include <stdint.h>
511558Srgrimes#include <string.h>
5223675Speter
531558Srgrimes#include "fsck.h"
541558Srgrimes
551558Srgrimes#define MINDIRSIZE	(sizeof (struct dirtemplate))
561558Srgrimes
5792839Simpstatic int blksort(const void *, const void *);
5892839Simpstatic int pass2check(struct inodesc *);
591558Srgrimes
607585Sbdevoid
6192839Simppass2(void)
621558Srgrimes{
6398542Smckusick	union dinode *dp;
6492806Sobrien	struct inoinfo **inpp, *inp;
651558Srgrimes	struct inoinfo **inpend;
661558Srgrimes	struct inodesc curino;
6798542Smckusick	union dinode dino;
6898542Smckusick	int i;
691558Srgrimes	char pathbuf[MAXPATHLEN + 1];
701558Srgrimes
7141474Sjulian	switch (inoinfo(ROOTINO)->ino_state) {
721558Srgrimes
731558Srgrimes	case USTATE:
741558Srgrimes		pfatal("ROOT INODE UNALLOCATED");
7534266Sjulian		if (reply("ALLOCATE") == 0) {
7634266Sjulian			ckfini(0);
7723675Speter			exit(EEXIT);
7834266Sjulian		}
791558Srgrimes		if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
8023675Speter			errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
811558Srgrimes		break;
821558Srgrimes
831558Srgrimes	case DCLEAR:
841558Srgrimes		pfatal("DUPS/BAD IN ROOT INODE");
851558Srgrimes		if (reply("REALLOCATE")) {
861558Srgrimes			freeino(ROOTINO);
871558Srgrimes			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
8823675Speter				errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
891558Srgrimes			break;
901558Srgrimes		}
9134266Sjulian		if (reply("CONTINUE") == 0) {
9234266Sjulian			ckfini(0);
9323675Speter			exit(EEXIT);
9434266Sjulian		}
951558Srgrimes		break;
961558Srgrimes
971558Srgrimes	case FSTATE:
981558Srgrimes	case FCLEAR:
991558Srgrimes		pfatal("ROOT INODE NOT DIRECTORY");
1001558Srgrimes		if (reply("REALLOCATE")) {
1011558Srgrimes			freeino(ROOTINO);
1021558Srgrimes			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
10323675Speter				errx(EEXIT, "CANNOT ALLOCATE ROOT INODE");
1041558Srgrimes			break;
1051558Srgrimes		}
10634266Sjulian		if (reply("FIX") == 0) {
10734266Sjulian			ckfini(0);
10823675Speter			exit(EEXIT);
10934266Sjulian		}
1101558Srgrimes		dp = ginode(ROOTINO);
11198542Smckusick		DIP(dp, di_mode) &= ~IFMT;
11298542Smckusick		DIP(dp, di_mode) |= IFDIR;
1131558Srgrimes		inodirty();
1141558Srgrimes		break;
1151558Srgrimes
1161558Srgrimes	case DSTATE:
1171558Srgrimes		break;
1181558Srgrimes
1191558Srgrimes	default:
12041474Sjulian		errx(EEXIT, "BAD STATE %d FOR ROOT INODE",
12141474Sjulian		    inoinfo(ROOTINO)->ino_state);
1221558Srgrimes	}
12341474Sjulian	inoinfo(ROOTINO)->ino_state = DFOUND;
12496483Sphk	inoinfo(WINO)->ino_state = FSTATE;
12596483Sphk	inoinfo(WINO)->ino_type = DT_WHT;
1261558Srgrimes	/*
1271558Srgrimes	 * Sort the directory list into disk block order.
1281558Srgrimes	 */
1291558Srgrimes	qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
1301558Srgrimes	/*
1311558Srgrimes	 * Check the integrity of each directory.
1321558Srgrimes	 */
13323675Speter	memset(&curino, 0, sizeof(struct inodesc));
1341558Srgrimes	curino.id_type = DATA;
1351558Srgrimes	curino.id_func = pass2check;
1361558Srgrimes	inpend = &inpsort[inplast];
1371558Srgrimes	for (inpp = inpsort; inpp < inpend; inpp++) {
13870050Siedowse		if (got_siginfo) {
139101037Smux			printf("%s: phase 2: dir %td of %d (%d%%)\n", cdevname,
14086514Siedowse			    inpp - inpsort, (int)inplast,
14186514Siedowse			    (int)((inpp - inpsort) * 100 / inplast));
14270050Siedowse			got_siginfo = 0;
14370050Siedowse		}
1441558Srgrimes		inp = *inpp;
1451558Srgrimes		if (inp->i_isize == 0)
1461558Srgrimes			continue;
1471558Srgrimes		if (inp->i_isize < MINDIRSIZE) {
1481558Srgrimes			direrror(inp->i_number, "DIRECTORY TOO SHORT");
1491558Srgrimes			inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
1501558Srgrimes			if (reply("FIX") == 1) {
1511558Srgrimes				dp = ginode(inp->i_number);
15298542Smckusick				DIP(dp, di_size) = inp->i_isize;
1531558Srgrimes				inodirty();
1541558Srgrimes			}
1551558Srgrimes		} else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
1561558Srgrimes			getpathname(pathbuf, inp->i_number, inp->i_number);
15734266Sjulian			if (usedsoftdep)
158101037Smux				pfatal("%s %s: LENGTH %jd NOT MULTIPLE OF %d",
159101037Smux					"DIRECTORY", pathbuf,
160101037Smux					(intmax_t)inp->i_isize, DIRBLKSIZ);
16134266Sjulian			else
162101037Smux				pwarn("%s %s: LENGTH %jd NOT MULTIPLE OF %d",
163101037Smux					"DIRECTORY", pathbuf,
164101037Smux					(intmax_t)inp->i_isize, DIRBLKSIZ);
1651558Srgrimes			if (preen)
1661558Srgrimes				printf(" (ADJUSTED)\n");
1671558Srgrimes			inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
1681558Srgrimes			if (preen || reply("ADJUST") == 1) {
1691558Srgrimes				dp = ginode(inp->i_number);
17098542Smckusick				DIP(dp, di_size) =
17198542Smckusick				    roundup(inp->i_isize, DIRBLKSIZ);
1721558Srgrimes				inodirty();
1731558Srgrimes			}
1741558Srgrimes		}
17598542Smckusick		dp = &dino;
17698542Smckusick		memset(dp, 0, sizeof(struct ufs2_dinode));
17798542Smckusick		DIP(dp, di_mode) = IFDIR;
17898542Smckusick		DIP(dp, di_size) = inp->i_isize;
17998542Smckusick		for (i = 0;
18098542Smckusick		     i < (inp->i_numblks<NDADDR ? inp->i_numblks : NDADDR);
18198542Smckusick		     i++)
18298542Smckusick			DIP(dp, di_db[i]) = inp->i_blks[i];
18398542Smckusick		if (inp->i_numblks > NDADDR)
18498542Smckusick			for (i = 0; i < NIADDR; i++)
18598542Smckusick				DIP(dp, di_ib[i]) = inp->i_blks[NDADDR + i];
1861558Srgrimes		curino.id_number = inp->i_number;
1871558Srgrimes		curino.id_parent = inp->i_parent;
1881558Srgrimes		(void)ckinode(dp, &curino);
1891558Srgrimes	}
1901558Srgrimes	/*
1911558Srgrimes	 * Now that the parents of all directories have been found,
1921558Srgrimes	 * make another pass to verify the value of `..'
1931558Srgrimes	 */
1941558Srgrimes	for (inpp = inpsort; inpp < inpend; inpp++) {
1951558Srgrimes		inp = *inpp;
1961558Srgrimes		if (inp->i_parent == 0 || inp->i_isize == 0)
1971558Srgrimes			continue;
19841474Sjulian		if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
19941474Sjulian		    inoinfo(inp->i_number)->ino_state == DSTATE)
20041474Sjulian			inoinfo(inp->i_number)->ino_state = DFOUND;
2011558Srgrimes		if (inp->i_dotdot == inp->i_parent ||
2021558Srgrimes		    inp->i_dotdot == (ino_t)-1)
2031558Srgrimes			continue;
2041558Srgrimes		if (inp->i_dotdot == 0) {
2051558Srgrimes			inp->i_dotdot = inp->i_parent;
2061558Srgrimes			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
2071558Srgrimes			if (reply("FIX") == 0)
2081558Srgrimes				continue;
2091558Srgrimes			(void)makeentry(inp->i_number, inp->i_parent, "..");
21041474Sjulian			inoinfo(inp->i_parent)->ino_linkcnt--;
2111558Srgrimes			continue;
2121558Srgrimes		}
2131558Srgrimes		fileerror(inp->i_parent, inp->i_number,
2141558Srgrimes		    "BAD INODE NUMBER FOR '..'");
2151558Srgrimes		if (reply("FIX") == 0)
2161558Srgrimes			continue;
21741474Sjulian		inoinfo(inp->i_dotdot)->ino_linkcnt++;
21841474Sjulian		inoinfo(inp->i_parent)->ino_linkcnt--;
2191558Srgrimes		inp->i_dotdot = inp->i_parent;
2201558Srgrimes		(void)changeino(inp->i_number, "..", inp->i_parent);
2211558Srgrimes	}
2221558Srgrimes	/*
2231558Srgrimes	 * Mark all the directories that can be found from the root.
2241558Srgrimes	 */
2251558Srgrimes	propagate();
2261558Srgrimes}
2271558Srgrimes
22823675Speterstatic int
22992839Simppass2check(struct inodesc *idesc)
2301558Srgrimes{
23192806Sobrien	struct direct *dirp = idesc->id_dirp;
23292806Sobrien	struct inoinfo *inp;
2331558Srgrimes	int n, entrysize, ret = 0;
23498542Smckusick	union dinode *dp;
235100935Sphk	const char *errmsg;
2361558Srgrimes	struct direct proto;
2371558Srgrimes	char namebuf[MAXPATHLEN + 1];
2381558Srgrimes	char pathbuf[MAXPATHLEN + 1];
2391558Srgrimes
2401558Srgrimes	/*
2411558Srgrimes	 * check for "."
2421558Srgrimes	 */
2431558Srgrimes	if (idesc->id_entryno != 0)
2441558Srgrimes		goto chk1;
2451558Srgrimes	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
2461558Srgrimes		if (dirp->d_ino != idesc->id_number) {
2471558Srgrimes			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
2481558Srgrimes			dirp->d_ino = idesc->id_number;
2491558Srgrimes			if (reply("FIX") == 1)
2501558Srgrimes				ret |= ALTERED;
2511558Srgrimes		}
25296483Sphk		if (dirp->d_type != DT_DIR) {
2531558Srgrimes			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
2541558Srgrimes			dirp->d_type = DT_DIR;
2551558Srgrimes			if (reply("FIX") == 1)
2561558Srgrimes				ret |= ALTERED;
2571558Srgrimes		}
2581558Srgrimes		goto chk1;
2591558Srgrimes	}
2601558Srgrimes	direrror(idesc->id_number, "MISSING '.'");
2611558Srgrimes	proto.d_ino = idesc->id_number;
26296483Sphk	proto.d_type = DT_DIR;
2631558Srgrimes	proto.d_namlen = 1;
2641558Srgrimes	(void)strcpy(proto.d_name, ".");
2651558Srgrimes	entrysize = DIRSIZ(0, &proto);
2661558Srgrimes	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
2671558Srgrimes		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
2681558Srgrimes			dirp->d_name);
2691558Srgrimes	} else if (dirp->d_reclen < entrysize) {
2701558Srgrimes		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
2711558Srgrimes	} else if (dirp->d_reclen < 2 * entrysize) {
2721558Srgrimes		proto.d_reclen = dirp->d_reclen;
27323675Speter		memmove(dirp, &proto, (size_t)entrysize);
2741558Srgrimes		if (reply("FIX") == 1)
2751558Srgrimes			ret |= ALTERED;
2761558Srgrimes	} else {
2771558Srgrimes		n = dirp->d_reclen - entrysize;
2781558Srgrimes		proto.d_reclen = entrysize;
27923675Speter		memmove(dirp, &proto, (size_t)entrysize);
2801558Srgrimes		idesc->id_entryno++;
28141474Sjulian		inoinfo(dirp->d_ino)->ino_linkcnt--;
2821558Srgrimes		dirp = (struct direct *)((char *)(dirp) + entrysize);
28323675Speter		memset(dirp, 0, (size_t)n);
2841558Srgrimes		dirp->d_reclen = n;
2851558Srgrimes		if (reply("FIX") == 1)
2861558Srgrimes			ret |= ALTERED;
2871558Srgrimes	}
2881558Srgrimeschk1:
2891558Srgrimes	if (idesc->id_entryno > 1)
2901558Srgrimes		goto chk2;
2911558Srgrimes	inp = getinoinfo(idesc->id_number);
2921558Srgrimes	proto.d_ino = inp->i_parent;
29396483Sphk	proto.d_type = DT_DIR;
2941558Srgrimes	proto.d_namlen = 2;
2951558Srgrimes	(void)strcpy(proto.d_name, "..");
2961558Srgrimes	entrysize = DIRSIZ(0, &proto);
2971558Srgrimes	if (idesc->id_entryno == 0) {
2981558Srgrimes		n = DIRSIZ(0, dirp);
2991558Srgrimes		if (dirp->d_reclen < n + entrysize)
3001558Srgrimes			goto chk2;
3011558Srgrimes		proto.d_reclen = dirp->d_reclen - n;
3021558Srgrimes		dirp->d_reclen = n;
3031558Srgrimes		idesc->id_entryno++;
30441474Sjulian		inoinfo(dirp->d_ino)->ino_linkcnt--;
3051558Srgrimes		dirp = (struct direct *)((char *)(dirp) + n);
30623675Speter		memset(dirp, 0, (size_t)proto.d_reclen);
3071558Srgrimes		dirp->d_reclen = proto.d_reclen;
3081558Srgrimes	}
3091558Srgrimes	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
3101558Srgrimes		inp->i_dotdot = dirp->d_ino;
31196483Sphk		if (dirp->d_type != DT_DIR) {
3121558Srgrimes			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
3131558Srgrimes			dirp->d_type = DT_DIR;
3141558Srgrimes			if (reply("FIX") == 1)
3151558Srgrimes				ret |= ALTERED;
3161558Srgrimes		}
3171558Srgrimes		goto chk2;
3181558Srgrimes	}
3191558Srgrimes	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
3201558Srgrimes		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
3211558Srgrimes		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
3221558Srgrimes			dirp->d_name);
3231558Srgrimes		inp->i_dotdot = (ino_t)-1;
3241558Srgrimes	} else if (dirp->d_reclen < entrysize) {
3251558Srgrimes		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
3261558Srgrimes		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
3271558Srgrimes		inp->i_dotdot = (ino_t)-1;
3281558Srgrimes	} else if (inp->i_parent != 0) {
3291558Srgrimes		/*
3301558Srgrimes		 * We know the parent, so fix now.
3311558Srgrimes		 */
3321558Srgrimes		inp->i_dotdot = inp->i_parent;
3331558Srgrimes		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
3341558Srgrimes		proto.d_reclen = dirp->d_reclen;
33523675Speter		memmove(dirp, &proto, (size_t)entrysize);
3361558Srgrimes		if (reply("FIX") == 1)
3371558Srgrimes			ret |= ALTERED;
3381558Srgrimes	}
3391558Srgrimes	idesc->id_entryno++;
3401558Srgrimes	if (dirp->d_ino != 0)
34141474Sjulian		inoinfo(dirp->d_ino)->ino_linkcnt--;
3421558Srgrimes	return (ret|KEEPON);
3431558Srgrimeschk2:
3441558Srgrimes	if (dirp->d_ino == 0)
3451558Srgrimes		return (ret|KEEPON);
3461558Srgrimes	if (dirp->d_namlen <= 2 &&
3471558Srgrimes	    dirp->d_name[0] == '.' &&
3481558Srgrimes	    idesc->id_entryno >= 2) {
3491558Srgrimes		if (dirp->d_namlen == 1) {
3501558Srgrimes			direrror(idesc->id_number, "EXTRA '.' ENTRY");
3511558Srgrimes			dirp->d_ino = 0;
3521558Srgrimes			if (reply("FIX") == 1)
3531558Srgrimes				ret |= ALTERED;
3541558Srgrimes			return (KEEPON | ret);
3551558Srgrimes		}
3561558Srgrimes		if (dirp->d_name[1] == '.') {
3571558Srgrimes			direrror(idesc->id_number, "EXTRA '..' ENTRY");
3581558Srgrimes			dirp->d_ino = 0;
3591558Srgrimes			if (reply("FIX") == 1)
3601558Srgrimes				ret |= ALTERED;
3611558Srgrimes			return (KEEPON | ret);
3621558Srgrimes		}
3631558Srgrimes	}
3641558Srgrimes	idesc->id_entryno++;
3651558Srgrimes	n = 0;
3661558Srgrimes	if (dirp->d_ino > maxino) {
3671558Srgrimes		fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
3681558Srgrimes		n = reply("REMOVE");
36996483Sphk	} else if (((dirp->d_ino == WINO && dirp->d_type != DT_WHT) ||
37023675Speter		    (dirp->d_ino != WINO && dirp->d_type == DT_WHT))) {
37123675Speter		fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
37223675Speter		dirp->d_ino = WINO;
37323675Speter		dirp->d_type = DT_WHT;
37423675Speter		if (reply("FIX") == 1)
37523675Speter			ret |= ALTERED;
3761558Srgrimes	} else {
3771558Srgrimesagain:
37841474Sjulian		switch (inoinfo(dirp->d_ino)->ino_state) {
3791558Srgrimes		case USTATE:
3801558Srgrimes			if (idesc->id_entryno <= 2)
3811558Srgrimes				break;
3821558Srgrimes			fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
3831558Srgrimes			n = reply("REMOVE");
3841558Srgrimes			break;
3851558Srgrimes
3861558Srgrimes		case DCLEAR:
3871558Srgrimes		case FCLEAR:
3881558Srgrimes			if (idesc->id_entryno <= 2)
3891558Srgrimes				break;
39041474Sjulian			if (inoinfo(dirp->d_ino)->ino_state == FCLEAR)
3911558Srgrimes				errmsg = "DUP/BAD";
39234266Sjulian			else if (!preen && !usedsoftdep)
3931558Srgrimes				errmsg = "ZERO LENGTH DIRECTORY";
3941558Srgrimes			else {
3951558Srgrimes				n = 1;
3961558Srgrimes				break;
3971558Srgrimes			}
3981558Srgrimes			fileerror(idesc->id_number, dirp->d_ino, errmsg);
3991558Srgrimes			if ((n = reply("REMOVE")) == 1)
4001558Srgrimes				break;
4011558Srgrimes			dp = ginode(dirp->d_ino);
40241474Sjulian			inoinfo(dirp->d_ino)->ino_state =
40398542Smckusick			   (DIP(dp, di_mode) & IFMT) == IFDIR ? DSTATE : FSTATE;
40498542Smckusick			inoinfo(dirp->d_ino)->ino_linkcnt = DIP(dp, di_nlink);
4051558Srgrimes			goto again;
4061558Srgrimes
4071558Srgrimes		case DSTATE:
40841474Sjulian			if (inoinfo(idesc->id_number)->ino_state == DFOUND)
40941474Sjulian				inoinfo(dirp->d_ino)->ino_state = DFOUND;
410102411Scharnier			/* FALLTHROUGH */
4111558Srgrimes
4121558Srgrimes		case DFOUND:
4131558Srgrimes			inp = getinoinfo(dirp->d_ino);
4141558Srgrimes			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
4151558Srgrimes				getpathname(pathbuf, idesc->id_number,
4161558Srgrimes				    idesc->id_number);
4171558Srgrimes				getpathname(namebuf, dirp->d_ino, dirp->d_ino);
41886512Siedowse				pwarn("%s%s%s %s %s\n", pathbuf,
41986512Siedowse				    (strcmp(pathbuf, "/") == 0 ? "" : "/"),
42086512Siedowse				    dirp->d_name,
4211558Srgrimes				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
4221558Srgrimes				    namebuf);
42374556Smckusick				if (cursnapshot != 0)
42474556Smckusick					break;
42534266Sjulian				if (preen) {
42634266Sjulian					printf(" (REMOVED)\n");
42741474Sjulian					n = 1;
42841474Sjulian					break;
42934266Sjulian				}
43041474Sjulian				if ((n = reply("REMOVE")) == 1)
4311558Srgrimes					break;
4321558Srgrimes			}
4331558Srgrimes			if (idesc->id_entryno > 2)
4341558Srgrimes				inp->i_parent = idesc->id_number;
435102411Scharnier			/* FALLTHROUGH */
4361558Srgrimes
4371558Srgrimes		case FSTATE:
43896483Sphk			if (dirp->d_type != inoinfo(dirp->d_ino)->ino_type) {
4391558Srgrimes				fileerror(idesc->id_number, dirp->d_ino,
4401558Srgrimes				    "BAD TYPE VALUE");
44141474Sjulian				dirp->d_type = inoinfo(dirp->d_ino)->ino_type;
4421558Srgrimes				if (reply("FIX") == 1)
4431558Srgrimes					ret |= ALTERED;
4441558Srgrimes			}
44541474Sjulian			inoinfo(dirp->d_ino)->ino_linkcnt--;
4461558Srgrimes			break;
4471558Srgrimes
4481558Srgrimes		default:
44923675Speter			errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
45041474Sjulian			    inoinfo(dirp->d_ino)->ino_state, dirp->d_ino);
4511558Srgrimes		}
4521558Srgrimes	}
4531558Srgrimes	if (n == 0)
4541558Srgrimes		return (ret|KEEPON);
4551558Srgrimes	dirp->d_ino = 0;
4561558Srgrimes	return (ret|KEEPON|ALTERED);
4571558Srgrimes}
4581558Srgrimes
4591558Srgrimes/*
4601558Srgrimes * Routine to sort disk blocks.
4611558Srgrimes */
46223675Speterstatic int
46392839Simpblksort(const void *arg1, const void *arg2)
4641558Srgrimes{
4651558Srgrimes
466100935Sphk	return ((*(struct inoinfo * const *)arg1)->i_blks[0] -
467100935Sphk		(*(struct inoinfo * const *)arg2)->i_blks[0]);
4681558Srgrimes}
469