Home | History | Annotate | Line # | Download | only in fsck_lfs
pass2.c revision 1.7
      1  1.7  perseant /* $NetBSD: pass2.c,v 1.7 2003/03/28 08:09:54 perseant Exp $	 */
      2  1.1  perseant 
      3  1.1  perseant /*
      4  1.1  perseant  * Copyright (c) 1980, 1986, 1993
      5  1.1  perseant  *	The Regents of the University of California.  All rights reserved.
      6  1.1  perseant  *
      7  1.1  perseant  * Redistribution and use in source and binary forms, with or without
      8  1.1  perseant  * modification, are permitted provided that the following conditions
      9  1.1  perseant  * are met:
     10  1.1  perseant  * 1. Redistributions of source code must retain the above copyright
     11  1.1  perseant  *    notice, this list of conditions and the following disclaimer.
     12  1.1  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  perseant  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  perseant  *    documentation and/or other materials provided with the distribution.
     15  1.1  perseant  * 3. All advertising materials mentioning features or use of this software
     16  1.1  perseant  *    must display the following acknowledgement:
     17  1.1  perseant  *	This product includes software developed by the University of
     18  1.1  perseant  *	California, Berkeley and its contributors.
     19  1.1  perseant  * 4. Neither the name of the University nor the names of its contributors
     20  1.1  perseant  *    may be used to endorse or promote products derived from this software
     21  1.1  perseant  *    without specific prior written permission.
     22  1.1  perseant  *
     23  1.1  perseant  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  perseant  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  perseant  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  perseant  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  perseant  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  perseant  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  perseant  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  perseant  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  perseant  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  perseant  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  perseant  * SUCH DAMAGE.
     34  1.1  perseant  */
     35  1.1  perseant 
     36  1.7  perseant #include <sys/types.h>
     37  1.1  perseant #include <sys/param.h>
     38  1.1  perseant #include <sys/time.h>
     39  1.7  perseant #include <sys/mount.h>
     40  1.7  perseant #include <sys/buf.h>
     41  1.7  perseant 
     42  1.7  perseant #include <ufs/ufs/inode.h>
     43  1.1  perseant #include <ufs/ufs/dir.h>
     44  1.1  perseant #include <ufs/lfs/lfs.h>
     45  1.1  perseant 
     46  1.7  perseant #include <err.h>
     47  1.1  perseant #include <stdio.h>
     48  1.1  perseant #include <stdlib.h>
     49  1.1  perseant #include <string.h>
     50  1.1  perseant 
     51  1.7  perseant #include "bufcache.h"
     52  1.7  perseant #include "vnode.h"
     53  1.7  perseant #include "lfs.h"
     54  1.7  perseant 
     55  1.1  perseant #include "fsck.h"
     56  1.1  perseant #include "fsutil.h"
     57  1.1  perseant #include "extern.h"
     58  1.1  perseant 
     59  1.1  perseant #define MINDIRSIZE	(sizeof (struct dirtemplate))
     60  1.1  perseant 
     61  1.7  perseant static int pass2check(struct inodesc *);
     62  1.7  perseant static int blksort(const void *, const void *);
     63  1.1  perseant 
     64  1.1  perseant void
     65  1.1  perseant pass2()
     66  1.1  perseant {
     67  1.7  perseant 	struct dinode *dp;
     68  1.7  perseant 	struct uvnode *vp;
     69  1.7  perseant 	struct inoinfo **inpp, *inp;
     70  1.1  perseant 	struct inoinfo **inpend;
     71  1.7  perseant 	struct inodesc curino;
     72  1.7  perseant 	struct dinode dino;
     73  1.7  perseant 	char pathbuf[MAXPATHLEN + 1];
     74  1.1  perseant 
     75  1.1  perseant 	switch (statemap[ROOTINO]) {
     76  1.1  perseant 
     77  1.1  perseant 	case USTATE:
     78  1.1  perseant 		pfatal("ROOT INODE UNALLOCATED");
     79  1.1  perseant 		if (reply("ALLOCATE") == 0)
     80  1.7  perseant 			err(8, "%s", "");
     81  1.1  perseant 		if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
     82  1.7  perseant 			err(8, "CANNOT ALLOCATE ROOT INODE\n");
     83  1.1  perseant 		break;
     84  1.1  perseant 
     85  1.1  perseant 	case DCLEAR:
     86  1.1  perseant 		pfatal("DUPS/BAD IN ROOT INODE");
     87  1.1  perseant 		if (reply("REALLOCATE")) {
     88  1.1  perseant 			freeino(ROOTINO);
     89  1.1  perseant 			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
     90  1.7  perseant 				err(8, "CANNOT ALLOCATE ROOT INODE\n");
     91  1.1  perseant 			break;
     92  1.1  perseant 		}
     93  1.1  perseant 		if (reply("CONTINUE") == 0)
     94  1.7  perseant 			err(8, "%s", "");
     95  1.1  perseant 		break;
     96  1.1  perseant 
     97  1.1  perseant 	case FSTATE:
     98  1.1  perseant 	case FCLEAR:
     99  1.1  perseant 		pfatal("ROOT INODE NOT DIRECTORY");
    100  1.1  perseant 		if (reply("REALLOCATE")) {
    101  1.1  perseant 			freeino(ROOTINO);
    102  1.1  perseant 			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
    103  1.7  perseant 				err(8, "CANNOT ALLOCATE ROOT INODE\n");
    104  1.1  perseant 			break;
    105  1.1  perseant 		}
    106  1.1  perseant 		if (reply("FIX") == 0)
    107  1.7  perseant 			err(8, "%s", "");
    108  1.7  perseant 		vp = vget(fs, ROOTINO);
    109  1.7  perseant 		dp = VTOD(vp);
    110  1.1  perseant 		dp->di_mode &= ~IFMT;
    111  1.1  perseant 		dp->di_mode |= IFDIR;
    112  1.7  perseant 		inodirty(VTOI(vp));
    113  1.1  perseant 		break;
    114  1.1  perseant 
    115  1.1  perseant 	case DSTATE:
    116  1.1  perseant 		break;
    117  1.1  perseant 
    118  1.1  perseant 	default:
    119  1.7  perseant 		err(8, "BAD STATE %d FOR ROOT INODE\n", statemap[ROOTINO]);
    120  1.1  perseant 	}
    121  1.7  perseant 	statemap[WINO] = FSTATE;
    122  1.7  perseant 	typemap[WINO] = DT_WHT;
    123  1.1  perseant 	/*
    124  1.1  perseant 	 * Sort the directory list into disk block order.
    125  1.1  perseant 	 */
    126  1.7  perseant 	qsort((char *) inpsort, (size_t) inplast, sizeof *inpsort, blksort);
    127  1.1  perseant 	/*
    128  1.1  perseant 	 * Check the integrity of each directory.
    129  1.1  perseant 	 */
    130  1.1  perseant 	memset(&curino, 0, sizeof(struct inodesc));
    131  1.1  perseant 	curino.id_type = DATA;
    132  1.1  perseant 	curino.id_func = pass2check;
    133  1.1  perseant 	inpend = &inpsort[inplast];
    134  1.1  perseant 	for (inpp = inpsort; inpp < inpend; inpp++) {
    135  1.1  perseant 		inp = *inpp;
    136  1.1  perseant 		if (inp->i_isize == 0)
    137  1.1  perseant 			continue;
    138  1.1  perseant 		if (inp->i_isize < MINDIRSIZE) {
    139  1.1  perseant 			direrror(inp->i_number, "DIRECTORY TOO SHORT");
    140  1.1  perseant 			inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
    141  1.1  perseant 			if (reply("FIX") == 1) {
    142  1.7  perseant 				vp = vget(fs, inp->i_number);
    143  1.7  perseant 				dp = VTOD(vp);
    144  1.1  perseant 				dp->di_size = inp->i_isize;
    145  1.7  perseant 				inodirty(VTOI(vp));
    146  1.1  perseant 			}
    147  1.1  perseant 		} else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
    148  1.1  perseant 			getpathname(pathbuf, inp->i_number, inp->i_number);
    149  1.2   nathanw 			pwarn("DIRECTORY %s: LENGTH %lu NOT MULTIPLE OF %d",
    150  1.7  perseant 			    pathbuf, (unsigned long) inp->i_isize, DIRBLKSIZ);
    151  1.1  perseant 			if (preen)
    152  1.1  perseant 				printf(" (ADJUSTED)\n");
    153  1.1  perseant 			inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
    154  1.1  perseant 			if (preen || reply("ADJUST") == 1) {
    155  1.7  perseant 				vp = vget(fs, inp->i_number);
    156  1.7  perseant 				dp = VTOD(vp);
    157  1.1  perseant 				dp->di_size = inp->i_isize;
    158  1.7  perseant 				inodirty(VTOI(vp));
    159  1.1  perseant 			}
    160  1.1  perseant 		}
    161  1.1  perseant 		memset(&dino, 0, sizeof(struct dinode));
    162  1.1  perseant 		dino.di_mode = IFDIR;
    163  1.1  perseant 		dino.di_size = inp->i_isize;
    164  1.7  perseant 		memcpy(&dino.di_db[0], &inp->i_blks[0], (size_t) inp->i_numblks);
    165  1.1  perseant 		curino.id_number = inp->i_number;
    166  1.1  perseant 		curino.id_parent = inp->i_parent;
    167  1.7  perseant 		(void) ckinode(&dino, &curino);
    168  1.1  perseant 	}
    169  1.1  perseant 	/*
    170  1.1  perseant 	 * Now that the parents of all directories have been found,
    171  1.1  perseant 	 * make another pass to verify the value of `..'
    172  1.1  perseant 	 */
    173  1.1  perseant 	for (inpp = inpsort; inpp < inpend; inpp++) {
    174  1.1  perseant 		inp = *inpp;
    175  1.1  perseant 		if (inp->i_parent == 0 || inp->i_isize == 0)
    176  1.1  perseant 			continue;
    177  1.1  perseant 		if (inp->i_dotdot == inp->i_parent ||
    178  1.7  perseant 		    inp->i_dotdot == (ino_t) - 1)
    179  1.1  perseant 			continue;
    180  1.1  perseant 		if (inp->i_dotdot == 0) {
    181  1.1  perseant 			inp->i_dotdot = inp->i_parent;
    182  1.1  perseant 			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
    183  1.1  perseant 			if (reply("FIX") == 0)
    184  1.1  perseant 				continue;
    185  1.7  perseant 			(void) makeentry(inp->i_number, inp->i_parent, "..");
    186  1.1  perseant 			lncntp[inp->i_parent]--;
    187  1.1  perseant 			continue;
    188  1.1  perseant 		}
    189  1.1  perseant 		fileerror(inp->i_parent, inp->i_number,
    190  1.7  perseant 		    "BAD INODE NUMBER FOR '..'");
    191  1.1  perseant 		if (reply("FIX") == 0)
    192  1.1  perseant 			continue;
    193  1.1  perseant 		lncntp[inp->i_dotdot]++;
    194  1.1  perseant 		lncntp[inp->i_parent]--;
    195  1.1  perseant 		inp->i_dotdot = inp->i_parent;
    196  1.7  perseant 		(void) changeino(inp->i_number, "..", inp->i_parent);
    197  1.1  perseant 	}
    198  1.1  perseant 	/*
    199  1.1  perseant 	 * Mark all the directories that can be found from the root.
    200  1.1  perseant 	 */
    201  1.1  perseant 	propagate();
    202  1.1  perseant }
    203  1.1  perseant 
    204  1.1  perseant static int
    205  1.4  perseant pass2check(struct inodesc * idesc)
    206  1.1  perseant {
    207  1.1  perseant 	register struct direct *dirp = idesc->id_dirp;
    208  1.1  perseant 	register struct inoinfo *inp;
    209  1.7  perseant 	int n, entrysize, ret = 0;
    210  1.7  perseant 	struct dinode *dp;
    211  1.7  perseant 	char *errmsg;
    212  1.7  perseant 	struct direct proto;
    213  1.7  perseant 	char namebuf[MAXPATHLEN + 1];
    214  1.7  perseant 	char pathbuf[MAXPATHLEN + 1];
    215  1.1  perseant 
    216  1.1  perseant 	/*
    217  1.1  perseant 	 * check for "."
    218  1.1  perseant 	 */
    219  1.1  perseant 	if (idesc->id_entryno != 0)
    220  1.1  perseant 		goto chk1;
    221  1.1  perseant 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
    222  1.1  perseant 		if (dirp->d_ino != idesc->id_number) {
    223  1.1  perseant 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
    224  1.1  perseant 			dirp->d_ino = idesc->id_number;
    225  1.1  perseant 			if (reply("FIX") == 1)
    226  1.1  perseant 				ret |= ALTERED;
    227  1.1  perseant 		}
    228  1.7  perseant 		if (dirp->d_type != DT_DIR) {
    229  1.1  perseant 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
    230  1.1  perseant 			dirp->d_type = DT_DIR;
    231  1.1  perseant 			if (reply("FIX") == 1)
    232  1.1  perseant 				ret |= ALTERED;
    233  1.1  perseant 		}
    234  1.1  perseant 		goto chk1;
    235  1.1  perseant 	}
    236  1.1  perseant 	direrror(idesc->id_number, "MISSING '.'");
    237  1.1  perseant 	proto.d_ino = idesc->id_number;
    238  1.7  perseant 	proto.d_type = DT_DIR;
    239  1.1  perseant 	proto.d_namlen = 1;
    240  1.7  perseant 	(void) strcpy(proto.d_name, ".");
    241  1.1  perseant 	entrysize = DIRSIZ(0, &proto, 0);
    242  1.1  perseant 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
    243  1.1  perseant 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
    244  1.7  perseant 		    dirp->d_name);
    245  1.1  perseant 	} else if (dirp->d_reclen < entrysize) {
    246  1.1  perseant 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
    247  1.1  perseant 	} else if (dirp->d_reclen < 2 * entrysize) {
    248  1.1  perseant 		proto.d_reclen = dirp->d_reclen;
    249  1.7  perseant 		memcpy(dirp, &proto, (size_t) entrysize);
    250  1.1  perseant 		if (reply("FIX") == 1)
    251  1.1  perseant 			ret |= ALTERED;
    252  1.1  perseant 	} else {
    253  1.1  perseant 		n = dirp->d_reclen - entrysize;
    254  1.1  perseant 		proto.d_reclen = entrysize;
    255  1.7  perseant 		memcpy(dirp, &proto, (size_t) entrysize);
    256  1.1  perseant 		idesc->id_entryno++;
    257  1.1  perseant 		lncntp[dirp->d_ino]--;
    258  1.7  perseant 		dirp = (struct direct *) ((char *) (dirp) + entrysize);
    259  1.7  perseant 		memset(dirp, 0, (size_t) n);
    260  1.1  perseant 		dirp->d_reclen = n;
    261  1.1  perseant 		if (reply("FIX") == 1)
    262  1.1  perseant 			ret |= ALTERED;
    263  1.1  perseant 	}
    264  1.1  perseant chk1:
    265  1.1  perseant 	if (idesc->id_entryno > 1)
    266  1.1  perseant 		goto chk2;
    267  1.1  perseant 	inp = getinoinfo(idesc->id_number);
    268  1.1  perseant 	proto.d_ino = inp->i_parent;
    269  1.7  perseant 	proto.d_type = DT_DIR;
    270  1.1  perseant 	proto.d_namlen = 2;
    271  1.7  perseant 	(void) strcpy(proto.d_name, "..");
    272  1.1  perseant 	entrysize = DIRSIZ(0, &proto, 0);
    273  1.1  perseant 	if (idesc->id_entryno == 0) {
    274  1.1  perseant 		n = DIRSIZ(0, dirp, 0);
    275  1.1  perseant 		if (dirp->d_reclen < n + entrysize)
    276  1.1  perseant 			goto chk2;
    277  1.1  perseant 		proto.d_reclen = dirp->d_reclen - n;
    278  1.1  perseant 		dirp->d_reclen = n;
    279  1.1  perseant 		idesc->id_entryno++;
    280  1.1  perseant 		lncntp[dirp->d_ino]--;
    281  1.7  perseant 		dirp = (struct direct *) ((char *) (dirp) + n);
    282  1.7  perseant 		memset(dirp, 0, (size_t) proto.d_reclen);
    283  1.1  perseant 		dirp->d_reclen = proto.d_reclen;
    284  1.1  perseant 	}
    285  1.1  perseant 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
    286  1.1  perseant 		inp->i_dotdot = dirp->d_ino;
    287  1.7  perseant 		if (dirp->d_type != DT_DIR) {
    288  1.1  perseant 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
    289  1.1  perseant 			dirp->d_type = DT_DIR;
    290  1.1  perseant 			if (reply("FIX") == 1)
    291  1.1  perseant 				ret |= ALTERED;
    292  1.1  perseant 		}
    293  1.1  perseant 		goto chk2;
    294  1.1  perseant 	}
    295  1.1  perseant 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
    296  1.1  perseant 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    297  1.1  perseant 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
    298  1.7  perseant 		    dirp->d_name);
    299  1.7  perseant 		inp->i_dotdot = (ino_t) - 1;
    300  1.1  perseant 	} else if (dirp->d_reclen < entrysize) {
    301  1.1  perseant 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    302  1.1  perseant 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
    303  1.7  perseant 		inp->i_dotdot = (ino_t) - 1;
    304  1.1  perseant 	} else if (inp->i_parent != 0) {
    305  1.1  perseant 		/*
    306  1.1  perseant 		 * We know the parent, so fix now.
    307  1.1  perseant 		 */
    308  1.1  perseant 		inp->i_dotdot = inp->i_parent;
    309  1.1  perseant 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    310  1.1  perseant 		proto.d_reclen = dirp->d_reclen;
    311  1.7  perseant 		memcpy(dirp, &proto, (size_t) entrysize);
    312  1.1  perseant 		if (reply("FIX") == 1)
    313  1.1  perseant 			ret |= ALTERED;
    314  1.1  perseant 	}
    315  1.1  perseant 	idesc->id_entryno++;
    316  1.1  perseant 	if (dirp->d_ino != 0)
    317  1.1  perseant 		lncntp[dirp->d_ino]--;
    318  1.4  perseant 	return (ret | KEEPON);
    319  1.1  perseant chk2:
    320  1.1  perseant 	if (dirp->d_ino == 0)
    321  1.4  perseant 		return (ret | KEEPON);
    322  1.1  perseant 	if (dirp->d_namlen <= 2 &&
    323  1.1  perseant 	    dirp->d_name[0] == '.' &&
    324  1.1  perseant 	    idesc->id_entryno >= 2) {
    325  1.1  perseant 		if (dirp->d_namlen == 1) {
    326  1.1  perseant 			direrror(idesc->id_number, "EXTRA '.' ENTRY");
    327  1.1  perseant 			dirp->d_ino = 0;
    328  1.1  perseant 			if (reply("FIX") == 1)
    329  1.1  perseant 				ret |= ALTERED;
    330  1.1  perseant 			return (KEEPON | ret);
    331  1.1  perseant 		}
    332  1.1  perseant 		if (dirp->d_name[1] == '.') {
    333  1.1  perseant 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
    334  1.1  perseant 			dirp->d_ino = 0;
    335  1.1  perseant 			if (reply("FIX") == 1)
    336  1.1  perseant 				ret |= ALTERED;
    337  1.1  perseant 			return (KEEPON | ret);
    338  1.1  perseant 		}
    339  1.1  perseant 	}
    340  1.1  perseant 	idesc->id_entryno++;
    341  1.1  perseant 	n = 0;
    342  1.5  perseant 	if (dirp->d_ino >= maxino) {
    343  1.1  perseant 		fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
    344  1.1  perseant 		n = reply("REMOVE");
    345  1.4  perseant 	} else if (dirp->d_ino == LFS_IFILE_INUM &&
    346  1.7  perseant 	    idesc->id_number == ROOTINO) {
    347  1.4  perseant 		if (dirp->d_type != DT_REG) {
    348  1.4  perseant 			fileerror(idesc->id_number, dirp->d_ino,
    349  1.7  perseant 			    "BAD TYPE FOR IFILE");
    350  1.4  perseant 			dirp->d_type = DT_REG;
    351  1.4  perseant 			if (reply("FIX") == 1)
    352  1.4  perseant 				ret |= ALTERED;
    353  1.4  perseant 		}
    354  1.7  perseant 	} else if (((dirp->d_ino == WINO && (dirp->d_type != DT_WHT)) ||
    355  1.7  perseant 		(dirp->d_ino != WINO && dirp->d_type == DT_WHT))) {
    356  1.1  perseant 		fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
    357  1.1  perseant 		dirp->d_ino = WINO;
    358  1.1  perseant 		dirp->d_type = DT_WHT;
    359  1.1  perseant 		if (reply("FIX") == 1)
    360  1.1  perseant 			ret |= ALTERED;
    361  1.1  perseant 	} else {
    362  1.1  perseant again:
    363  1.1  perseant 		switch (statemap[dirp->d_ino]) {
    364  1.1  perseant 		case USTATE:
    365  1.1  perseant 			if (idesc->id_entryno <= 2)
    366  1.1  perseant 				break;
    367  1.1  perseant 			fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
    368  1.1  perseant 			n = reply("REMOVE");
    369  1.1  perseant 			break;
    370  1.1  perseant 
    371  1.1  perseant 		case DCLEAR:
    372  1.1  perseant 		case FCLEAR:
    373  1.1  perseant 			if (idesc->id_entryno <= 2)
    374  1.1  perseant 				break;
    375  1.1  perseant 			if (statemap[dirp->d_ino] == FCLEAR)
    376  1.1  perseant 				errmsg = "DUP/BAD";
    377  1.1  perseant 			else if (!preen)
    378  1.1  perseant 				errmsg = "ZERO LENGTH DIRECTORY";
    379  1.1  perseant 			else {
    380  1.1  perseant 				n = 1;
    381  1.1  perseant 				break;
    382  1.1  perseant 			}
    383  1.1  perseant 			fileerror(idesc->id_number, dirp->d_ino, errmsg);
    384  1.1  perseant 			if ((n = reply("REMOVE")) == 1)
    385  1.1  perseant 				break;
    386  1.1  perseant 			dp = ginode(dirp->d_ino);
    387  1.1  perseant 			statemap[dirp->d_ino] =
    388  1.7  perseant 			    (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE;
    389  1.1  perseant 			lncntp[dirp->d_ino] = dp->di_nlink;
    390  1.1  perseant 			goto again;
    391  1.1  perseant 
    392  1.1  perseant 		case DSTATE:
    393  1.1  perseant 		case DFOUND:
    394  1.1  perseant 			inp = getinoinfo(dirp->d_ino);
    395  1.1  perseant 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
    396  1.1  perseant 				getpathname(pathbuf, idesc->id_number,
    397  1.7  perseant 				    idesc->id_number);
    398  1.1  perseant 				getpathname(namebuf, dirp->d_ino, dirp->d_ino);
    399  1.1  perseant 				pwarn("%s %s %s\n", pathbuf,
    400  1.7  perseant 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
    401  1.7  perseant 				    namebuf);
    402  1.1  perseant 				if (preen)
    403  1.1  perseant 					printf(" (IGNORED)\n");
    404  1.1  perseant 				else if ((n = reply("REMOVE")) == 1)
    405  1.1  perseant 					break;
    406  1.1  perseant 			}
    407  1.1  perseant 			if (idesc->id_entryno > 2)
    408  1.1  perseant 				inp->i_parent = idesc->id_number;
    409  1.1  perseant 			/* fall through */
    410  1.1  perseant 
    411  1.1  perseant 		case FSTATE:
    412  1.7  perseant 			if (dirp->d_type != typemap[dirp->d_ino]) {
    413  1.1  perseant 				fileerror(idesc->id_number, dirp->d_ino,
    414  1.7  perseant 				    "BAD TYPE VALUE");
    415  1.1  perseant 				dirp->d_type = typemap[dirp->d_ino];
    416  1.1  perseant 				if (reply("FIX") == 1)
    417  1.1  perseant 					ret |= ALTERED;
    418  1.1  perseant 			}
    419  1.1  perseant 			lncntp[dirp->d_ino]--;
    420  1.1  perseant 			break;
    421  1.1  perseant 
    422  1.1  perseant 		default:
    423  1.7  perseant 			err(8, "BAD STATE %d FOR INODE I=%d",
    424  1.7  perseant 			    statemap[dirp->d_ino], dirp->d_ino);
    425  1.1  perseant 		}
    426  1.1  perseant 	}
    427  1.1  perseant 	if (n == 0)
    428  1.4  perseant 		return (ret | KEEPON);
    429  1.1  perseant 	dirp->d_ino = 0;
    430  1.4  perseant 	return (ret | KEEPON | ALTERED);
    431  1.1  perseant }
    432  1.1  perseant /*
    433  1.1  perseant  * Routine to sort disk blocks.
    434  1.1  perseant  */
    435  1.1  perseant static int
    436  1.4  perseant blksort(const void *inpp1, const void *inpp2)
    437  1.1  perseant {
    438  1.7  perseant 	return ((*(struct inoinfo **) inpp1)->i_blks[0] -
    439  1.7  perseant 	    (*(struct inoinfo **) inpp2)->i_blks[0]);
    440  1.1  perseant }
    441