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