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