Home | History | Annotate | Line # | Download | only in fsck_lfs
pass2.c revision 1.25
      1  1.25  christos /* $NetBSD: pass2.c,v 1.25 2015/06/16 23:58:30 christos 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.1  perseant #include <ufs/lfs/lfs.h>
     39  1.23  dholland #include <ufs/lfs/lfs_inode.h>
     40   1.1  perseant 
     41   1.7  perseant #include <err.h>
     42   1.1  perseant #include <stdio.h>
     43   1.1  perseant #include <stdlib.h>
     44   1.1  perseant #include <string.h>
     45   1.1  perseant 
     46   1.7  perseant #include "bufcache.h"
     47   1.7  perseant #include "vnode.h"
     48  1.15  christos #include "lfs_user.h"
     49   1.7  perseant 
     50   1.1  perseant #include "fsck.h"
     51   1.1  perseant #include "fsutil.h"
     52   1.1  perseant #include "extern.h"
     53   1.1  perseant 
     54  1.21  dholland #define MINDIRSIZE	(sizeof (struct lfs_dirtemplate))
     55   1.1  perseant 
     56   1.7  perseant static int pass2check(struct inodesc *);
     57   1.7  perseant static int blksort(const void *, const void *);
     58   1.1  perseant 
     59   1.1  perseant void
     60  1.11   xtraeme pass2(void)
     61   1.1  perseant {
     62  1.19  dholland 	struct ulfs1_dinode *dp;
     63   1.7  perseant 	struct uvnode *vp;
     64   1.7  perseant 	struct inoinfo **inpp, *inp;
     65   1.1  perseant 	struct inoinfo **inpend;
     66   1.7  perseant 	struct inodesc curino;
     67  1.19  dholland 	struct ulfs1_dinode dino;
     68   1.7  perseant 	char pathbuf[MAXPATHLEN + 1];
     69   1.1  perseant 
     70  1.19  dholland 	switch (statemap[ULFS_ROOTINO]) {
     71   1.1  perseant 
     72   1.1  perseant 	case USTATE:
     73   1.1  perseant 		pfatal("ROOT INODE UNALLOCATED");
     74   1.1  perseant 		if (reply("ALLOCATE") == 0)
     75  1.17  christos 			err(EEXIT, "%s", "");
     76  1.19  dholland 		if (allocdir(ULFS_ROOTINO, ULFS_ROOTINO, 0755) != ULFS_ROOTINO)
     77  1.25  christos 			err(EEXIT, "CANNOT ALLOCATE ROOT INODE");
     78   1.1  perseant 		break;
     79   1.1  perseant 
     80   1.1  perseant 	case DCLEAR:
     81   1.1  perseant 		pfatal("DUPS/BAD IN ROOT INODE");
     82   1.1  perseant 		if (reply("REALLOCATE")) {
     83  1.19  dholland 			freeino(ULFS_ROOTINO);
     84  1.19  dholland 			if (allocdir(ULFS_ROOTINO, ULFS_ROOTINO, 0755) != ULFS_ROOTINO)
     85  1.24  christos 				err(EEXIT, "CANNOT ALLOCATE ROOT INODE");
     86   1.1  perseant 			break;
     87   1.1  perseant 		}
     88   1.1  perseant 		if (reply("CONTINUE") == 0)
     89  1.17  christos 			err(EEXIT, "%s", "");
     90   1.1  perseant 		break;
     91   1.1  perseant 
     92   1.1  perseant 	case FSTATE:
     93   1.1  perseant 	case FCLEAR:
     94   1.1  perseant 		pfatal("ROOT INODE NOT DIRECTORY");
     95   1.1  perseant 		if (reply("REALLOCATE")) {
     96  1.19  dholland 			freeino(ULFS_ROOTINO);
     97  1.19  dholland 			if (allocdir(ULFS_ROOTINO, ULFS_ROOTINO, 0755) != ULFS_ROOTINO)
     98  1.24  christos 				err(EEXIT, "CANNOT ALLOCATE ROOT INODE");
     99   1.1  perseant 			break;
    100   1.1  perseant 		}
    101   1.1  perseant 		if (reply("FIX") == 0)
    102  1.17  christos 			errx(EEXIT, "%s", "");
    103  1.19  dholland 		vp = vget(fs, ULFS_ROOTINO);
    104   1.7  perseant 		dp = VTOD(vp);
    105  1.20  dholland 		dp->di_mode &= ~LFS_IFMT;
    106  1.20  dholland 		dp->di_mode |= LFS_IFDIR;
    107   1.7  perseant 		inodirty(VTOI(vp));
    108   1.1  perseant 		break;
    109   1.1  perseant 
    110   1.1  perseant 	case DSTATE:
    111   1.1  perseant 		break;
    112   1.1  perseant 
    113   1.1  perseant 	default:
    114  1.24  christos 		errx(EEXIT, "BAD STATE %d FOR ROOT INODE", statemap[ULFS_ROOTINO]);
    115   1.1  perseant 	}
    116  1.19  dholland 	statemap[ULFS_WINO] = FSTATE;
    117  1.21  dholland 	typemap[ULFS_WINO] = LFS_DT_WHT;
    118   1.1  perseant 	/*
    119   1.1  perseant 	 * Sort the directory list into disk block order.
    120   1.1  perseant 	 */
    121   1.7  perseant 	qsort((char *) inpsort, (size_t) inplast, sizeof *inpsort, blksort);
    122   1.1  perseant 	/*
    123   1.1  perseant 	 * Check the integrity of each directory.
    124   1.1  perseant 	 */
    125   1.1  perseant 	memset(&curino, 0, sizeof(struct inodesc));
    126   1.1  perseant 	curino.id_type = DATA;
    127   1.1  perseant 	curino.id_func = pass2check;
    128   1.1  perseant 	inpend = &inpsort[inplast];
    129   1.1  perseant 	for (inpp = inpsort; inpp < inpend; inpp++) {
    130   1.1  perseant 		inp = *inpp;
    131   1.1  perseant 		if (inp->i_isize == 0)
    132   1.1  perseant 			continue;
    133   1.1  perseant 		if (inp->i_isize < MINDIRSIZE) {
    134   1.1  perseant 			direrror(inp->i_number, "DIRECTORY TOO SHORT");
    135  1.22  dholland 			inp->i_isize = roundup(MINDIRSIZE, LFS_DIRBLKSIZ);
    136   1.1  perseant 			if (reply("FIX") == 1) {
    137   1.7  perseant 				vp = vget(fs, inp->i_number);
    138   1.7  perseant 				dp = VTOD(vp);
    139   1.1  perseant 				dp->di_size = inp->i_isize;
    140   1.7  perseant 				inodirty(VTOI(vp));
    141   1.1  perseant 			}
    142  1.22  dholland 		} else if ((inp->i_isize & (LFS_DIRBLKSIZ - 1)) != 0) {
    143   1.9    itojun 			getpathname(pathbuf, sizeof(pathbuf), inp->i_number,
    144   1.9    itojun 			    inp->i_number);
    145   1.2   nathanw 			pwarn("DIRECTORY %s: LENGTH %lu NOT MULTIPLE OF %d",
    146  1.22  dholland 			    pathbuf, (unsigned long) inp->i_isize, LFS_DIRBLKSIZ);
    147   1.1  perseant 			if (preen)
    148   1.1  perseant 				printf(" (ADJUSTED)\n");
    149  1.22  dholland 			inp->i_isize = roundup(inp->i_isize, LFS_DIRBLKSIZ);
    150   1.1  perseant 			if (preen || reply("ADJUST") == 1) {
    151   1.7  perseant 				vp = vget(fs, inp->i_number);
    152   1.7  perseant 				dp = VTOD(vp);
    153   1.1  perseant 				dp->di_size = inp->i_isize;
    154   1.7  perseant 				inodirty(VTOI(vp));
    155   1.1  perseant 			}
    156   1.1  perseant 		}
    157  1.19  dholland 		memset(&dino, 0, sizeof(struct ulfs1_dinode));
    158  1.20  dholland 		dino.di_mode = LFS_IFDIR;
    159   1.1  perseant 		dino.di_size = inp->i_isize;
    160   1.7  perseant 		memcpy(&dino.di_db[0], &inp->i_blks[0], (size_t) inp->i_numblks);
    161   1.1  perseant 		curino.id_number = inp->i_number;
    162   1.1  perseant 		curino.id_parent = inp->i_parent;
    163   1.7  perseant 		(void) ckinode(&dino, &curino);
    164   1.1  perseant 	}
    165   1.1  perseant 	/*
    166   1.1  perseant 	 * Now that the parents of all directories have been found,
    167   1.1  perseant 	 * make another pass to verify the value of `..'
    168   1.1  perseant 	 */
    169   1.1  perseant 	for (inpp = inpsort; inpp < inpend; inpp++) {
    170   1.1  perseant 		inp = *inpp;
    171   1.1  perseant 		if (inp->i_parent == 0 || inp->i_isize == 0)
    172   1.1  perseant 			continue;
    173   1.1  perseant 		if (inp->i_dotdot == inp->i_parent ||
    174   1.7  perseant 		    inp->i_dotdot == (ino_t) - 1)
    175   1.1  perseant 			continue;
    176   1.1  perseant 		if (inp->i_dotdot == 0) {
    177   1.1  perseant 			inp->i_dotdot = inp->i_parent;
    178   1.1  perseant 			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
    179   1.1  perseant 			if (reply("FIX") == 0)
    180   1.1  perseant 				continue;
    181   1.7  perseant 			(void) makeentry(inp->i_number, inp->i_parent, "..");
    182   1.1  perseant 			lncntp[inp->i_parent]--;
    183   1.1  perseant 			continue;
    184   1.1  perseant 		}
    185   1.1  perseant 		fileerror(inp->i_parent, inp->i_number,
    186   1.7  perseant 		    "BAD INODE NUMBER FOR '..'");
    187   1.1  perseant 		if (reply("FIX") == 0)
    188   1.1  perseant 			continue;
    189   1.1  perseant 		lncntp[inp->i_dotdot]++;
    190   1.1  perseant 		lncntp[inp->i_parent]--;
    191   1.1  perseant 		inp->i_dotdot = inp->i_parent;
    192   1.7  perseant 		(void) changeino(inp->i_number, "..", inp->i_parent);
    193   1.1  perseant 	}
    194   1.1  perseant 	/*
    195   1.1  perseant 	 * Mark all the directories that can be found from the root.
    196   1.1  perseant 	 */
    197   1.1  perseant 	propagate();
    198   1.1  perseant }
    199   1.1  perseant 
    200   1.1  perseant static int
    201   1.4  perseant pass2check(struct inodesc * idesc)
    202   1.1  perseant {
    203  1.21  dholland 	struct lfs_direct *dirp = idesc->id_dirp;
    204  1.12     perry 	struct inoinfo *inp;
    205   1.7  perseant 	int n, entrysize, ret = 0;
    206  1.19  dholland 	struct ulfs1_dinode *dp;
    207  1.14  christos 	const char *errmsg;
    208  1.21  dholland 	struct lfs_direct proto;
    209   1.7  perseant 	char namebuf[MAXPATHLEN + 1];
    210   1.7  perseant 	char pathbuf[MAXPATHLEN + 1];
    211   1.1  perseant 
    212   1.1  perseant 	/*
    213   1.1  perseant 	 * check for "."
    214   1.1  perseant 	 */
    215   1.1  perseant 	if (idesc->id_entryno != 0)
    216   1.1  perseant 		goto chk1;
    217   1.1  perseant 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
    218   1.1  perseant 		if (dirp->d_ino != idesc->id_number) {
    219   1.1  perseant 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
    220   1.1  perseant 			dirp->d_ino = idesc->id_number;
    221   1.1  perseant 			if (reply("FIX") == 1)
    222   1.1  perseant 				ret |= ALTERED;
    223   1.1  perseant 		}
    224  1.21  dholland 		if (dirp->d_type != LFS_DT_DIR) {
    225   1.1  perseant 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
    226  1.21  dholland 			dirp->d_type = LFS_DT_DIR;
    227   1.1  perseant 			if (reply("FIX") == 1)
    228   1.1  perseant 				ret |= ALTERED;
    229   1.1  perseant 		}
    230   1.1  perseant 		goto chk1;
    231   1.1  perseant 	}
    232   1.1  perseant 	direrror(idesc->id_number, "MISSING '.'");
    233   1.1  perseant 	proto.d_ino = idesc->id_number;
    234  1.21  dholland 	proto.d_type = LFS_DT_DIR;
    235   1.1  perseant 	proto.d_namlen = 1;
    236   1.9    itojun 	(void) strlcpy(proto.d_name, ".", sizeof(proto.d_name));
    237  1.22  dholland 	entrysize = LFS_DIRSIZ(0, &proto, 0);
    238   1.1  perseant 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
    239   1.1  perseant 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
    240   1.7  perseant 		    dirp->d_name);
    241   1.1  perseant 	} else if (dirp->d_reclen < entrysize) {
    242   1.1  perseant 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
    243   1.1  perseant 	} else if (dirp->d_reclen < 2 * entrysize) {
    244   1.1  perseant 		proto.d_reclen = dirp->d_reclen;
    245   1.7  perseant 		memcpy(dirp, &proto, (size_t) entrysize);
    246   1.1  perseant 		if (reply("FIX") == 1)
    247   1.1  perseant 			ret |= ALTERED;
    248   1.1  perseant 	} else {
    249   1.1  perseant 		n = dirp->d_reclen - entrysize;
    250   1.1  perseant 		proto.d_reclen = entrysize;
    251   1.7  perseant 		memcpy(dirp, &proto, (size_t) entrysize);
    252   1.1  perseant 		idesc->id_entryno++;
    253   1.1  perseant 		lncntp[dirp->d_ino]--;
    254  1.21  dholland 		dirp = (struct lfs_direct *) ((char *) (dirp) + entrysize);
    255   1.7  perseant 		memset(dirp, 0, (size_t) n);
    256   1.1  perseant 		dirp->d_reclen = n;
    257   1.1  perseant 		if (reply("FIX") == 1)
    258   1.1  perseant 			ret |= ALTERED;
    259   1.1  perseant 	}
    260   1.1  perseant chk1:
    261   1.1  perseant 	if (idesc->id_entryno > 1)
    262   1.1  perseant 		goto chk2;
    263   1.1  perseant 	inp = getinoinfo(idesc->id_number);
    264   1.1  perseant 	proto.d_ino = inp->i_parent;
    265  1.21  dholland 	proto.d_type = LFS_DT_DIR;
    266   1.1  perseant 	proto.d_namlen = 2;
    267   1.9    itojun 	(void) strlcpy(proto.d_name, "..", sizeof(proto.d_name));
    268  1.22  dholland 	entrysize = LFS_DIRSIZ(0, &proto, 0);
    269   1.1  perseant 	if (idesc->id_entryno == 0) {
    270  1.22  dholland 		n = LFS_DIRSIZ(0, dirp, 0);
    271   1.1  perseant 		if (dirp->d_reclen < n + entrysize)
    272   1.1  perseant 			goto chk2;
    273   1.1  perseant 		proto.d_reclen = dirp->d_reclen - n;
    274   1.1  perseant 		dirp->d_reclen = n;
    275   1.1  perseant 		idesc->id_entryno++;
    276   1.1  perseant 		lncntp[dirp->d_ino]--;
    277  1.21  dholland 		dirp = (struct lfs_direct *) ((char *) (dirp) + n);
    278   1.7  perseant 		memset(dirp, 0, (size_t) proto.d_reclen);
    279   1.1  perseant 		dirp->d_reclen = proto.d_reclen;
    280   1.1  perseant 	}
    281   1.1  perseant 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
    282   1.1  perseant 		inp->i_dotdot = dirp->d_ino;
    283  1.21  dholland 		if (dirp->d_type != LFS_DT_DIR) {
    284   1.1  perseant 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
    285  1.21  dholland 			dirp->d_type = LFS_DT_DIR;
    286   1.1  perseant 			if (reply("FIX") == 1)
    287   1.1  perseant 				ret |= ALTERED;
    288   1.1  perseant 		}
    289   1.1  perseant 		goto chk2;
    290   1.1  perseant 	}
    291   1.1  perseant 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
    292   1.1  perseant 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    293   1.1  perseant 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
    294   1.7  perseant 		    dirp->d_name);
    295   1.7  perseant 		inp->i_dotdot = (ino_t) - 1;
    296   1.1  perseant 	} else if (dirp->d_reclen < entrysize) {
    297   1.1  perseant 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    298   1.1  perseant 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
    299   1.7  perseant 		inp->i_dotdot = (ino_t) - 1;
    300   1.1  perseant 	} else if (inp->i_parent != 0) {
    301   1.1  perseant 		/*
    302   1.1  perseant 		 * We know the parent, so fix now.
    303   1.1  perseant 		 */
    304   1.1  perseant 		inp->i_dotdot = inp->i_parent;
    305   1.1  perseant 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    306   1.1  perseant 		proto.d_reclen = dirp->d_reclen;
    307   1.7  perseant 		memcpy(dirp, &proto, (size_t) entrysize);
    308   1.1  perseant 		if (reply("FIX") == 1)
    309   1.1  perseant 			ret |= ALTERED;
    310   1.1  perseant 	}
    311   1.1  perseant 	idesc->id_entryno++;
    312   1.1  perseant 	if (dirp->d_ino != 0)
    313   1.1  perseant 		lncntp[dirp->d_ino]--;
    314   1.4  perseant 	return (ret | KEEPON);
    315   1.1  perseant chk2:
    316   1.1  perseant 	if (dirp->d_ino == 0)
    317   1.4  perseant 		return (ret | KEEPON);
    318   1.1  perseant 	if (dirp->d_namlen <= 2 &&
    319   1.1  perseant 	    dirp->d_name[0] == '.' &&
    320   1.1  perseant 	    idesc->id_entryno >= 2) {
    321   1.1  perseant 		if (dirp->d_namlen == 1) {
    322   1.1  perseant 			direrror(idesc->id_number, "EXTRA '.' ENTRY");
    323   1.1  perseant 			dirp->d_ino = 0;
    324   1.1  perseant 			if (reply("FIX") == 1)
    325   1.1  perseant 				ret |= ALTERED;
    326   1.1  perseant 			return (KEEPON | ret);
    327   1.1  perseant 		}
    328   1.1  perseant 		if (dirp->d_name[1] == '.') {
    329   1.1  perseant 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
    330   1.1  perseant 			dirp->d_ino = 0;
    331   1.1  perseant 			if (reply("FIX") == 1)
    332   1.1  perseant 				ret |= ALTERED;
    333   1.1  perseant 			return (KEEPON | ret);
    334   1.1  perseant 		}
    335   1.1  perseant 	}
    336   1.1  perseant 	idesc->id_entryno++;
    337   1.1  perseant 	n = 0;
    338   1.5  perseant 	if (dirp->d_ino >= maxino) {
    339   1.1  perseant 		fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
    340   1.1  perseant 		n = reply("REMOVE");
    341   1.4  perseant 	} else if (dirp->d_ino == LFS_IFILE_INUM &&
    342  1.19  dholland 	    idesc->id_number == ULFS_ROOTINO) {
    343  1.21  dholland 		if (dirp->d_type != LFS_DT_REG) {
    344   1.4  perseant 			fileerror(idesc->id_number, dirp->d_ino,
    345   1.7  perseant 			    "BAD TYPE FOR IFILE");
    346  1.21  dholland 			dirp->d_type = LFS_DT_REG;
    347   1.4  perseant 			if (reply("FIX") == 1)
    348   1.4  perseant 				ret |= ALTERED;
    349   1.4  perseant 		}
    350  1.21  dholland 	} else if (((dirp->d_ino == ULFS_WINO && (dirp->d_type != LFS_DT_WHT)) ||
    351  1.21  dholland 		(dirp->d_ino != ULFS_WINO && dirp->d_type == LFS_DT_WHT))) {
    352   1.1  perseant 		fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
    353  1.19  dholland 		dirp->d_ino = ULFS_WINO;
    354  1.21  dholland 		dirp->d_type = LFS_DT_WHT;
    355   1.1  perseant 		if (reply("FIX") == 1)
    356   1.1  perseant 			ret |= ALTERED;
    357   1.1  perseant 	} else {
    358   1.1  perseant again:
    359   1.1  perseant 		switch (statemap[dirp->d_ino]) {
    360   1.1  perseant 		case USTATE:
    361   1.1  perseant 			if (idesc->id_entryno <= 2)
    362   1.1  perseant 				break;
    363   1.1  perseant 			fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
    364   1.1  perseant 			n = reply("REMOVE");
    365   1.1  perseant 			break;
    366   1.1  perseant 
    367   1.1  perseant 		case DCLEAR:
    368   1.1  perseant 		case FCLEAR:
    369   1.1  perseant 			if (idesc->id_entryno <= 2)
    370   1.1  perseant 				break;
    371   1.1  perseant 			if (statemap[dirp->d_ino] == FCLEAR)
    372   1.1  perseant 				errmsg = "DUP/BAD";
    373   1.1  perseant 			else if (!preen)
    374   1.1  perseant 				errmsg = "ZERO LENGTH DIRECTORY";
    375   1.1  perseant 			else {
    376   1.1  perseant 				n = 1;
    377   1.1  perseant 				break;
    378   1.1  perseant 			}
    379   1.1  perseant 			fileerror(idesc->id_number, dirp->d_ino, errmsg);
    380   1.1  perseant 			if ((n = reply("REMOVE")) == 1)
    381   1.1  perseant 				break;
    382   1.1  perseant 			dp = ginode(dirp->d_ino);
    383   1.1  perseant 			statemap[dirp->d_ino] =
    384  1.20  dholland 			    (dp->di_mode & LFS_IFMT) == LFS_IFDIR ? DSTATE : FSTATE;
    385   1.1  perseant 			lncntp[dirp->d_ino] = dp->di_nlink;
    386   1.1  perseant 			goto again;
    387   1.1  perseant 
    388   1.1  perseant 		case DSTATE:
    389   1.1  perseant 		case DFOUND:
    390   1.1  perseant 			inp = getinoinfo(dirp->d_ino);
    391   1.1  perseant 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
    392   1.9    itojun 				getpathname(pathbuf, sizeof(pathbuf),
    393   1.9    itojun 				    idesc->id_number, idesc->id_number);
    394   1.9    itojun 				getpathname(namebuf, sizeof(namebuf),
    395   1.9    itojun 				    dirp->d_ino, dirp->d_ino);
    396   1.1  perseant 				pwarn("%s %s %s\n", pathbuf,
    397   1.7  perseant 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
    398   1.7  perseant 				    namebuf);
    399   1.1  perseant 				if (preen)
    400   1.1  perseant 					printf(" (IGNORED)\n");
    401   1.1  perseant 				else if ((n = reply("REMOVE")) == 1)
    402   1.1  perseant 					break;
    403   1.1  perseant 			}
    404   1.1  perseant 			if (idesc->id_entryno > 2)
    405   1.1  perseant 				inp->i_parent = idesc->id_number;
    406   1.1  perseant 			/* fall through */
    407   1.1  perseant 
    408   1.1  perseant 		case FSTATE:
    409   1.7  perseant 			if (dirp->d_type != typemap[dirp->d_ino]) {
    410   1.1  perseant 				fileerror(idesc->id_number, dirp->d_ino,
    411   1.7  perseant 				    "BAD TYPE VALUE");
    412  1.13  perseant 				if (debug)
    413  1.13  perseant 					pwarn("dir has %d, typemap has %d\n",
    414  1.13  perseant 						dirp->d_type, typemap[dirp->d_ino]);
    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.17  christos 			errx(EEXIT, "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.14  christos 	return ((*(const struct inoinfo *const *) inpp1)->i_blks[0] -
    439  1.14  christos 	    (*(const struct inoinfo *const *) inpp2)->i_blks[0]);
    440   1.1  perseant }
    441