Home | History | Annotate | Line # | Download | only in fsck_lfs
pass2.c revision 1.34
      1  1.34  dholland /* $NetBSD: pass2.c,v 1.34 2015/09/21 01:24:23 dholland 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.26  dholland #include <ufs/lfs/lfs_accessors.h>
     40  1.23  dholland #include <ufs/lfs/lfs_inode.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.15  christos #include "lfs_user.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.34  dholland #define MINDIRSIZE(fs) \
     56  1.34  dholland 	((fs)->lfs_is64 ? sizeof(struct lfs_dirtemplate64) : \
     57  1.34  dholland 			sizeof(struct lfs_dirtemplate32))
     58   1.1  perseant 
     59   1.7  perseant static int pass2check(struct inodesc *);
     60   1.7  perseant static int blksort(const void *, const void *);
     61   1.1  perseant 
     62   1.1  perseant void
     63  1.11   xtraeme pass2(void)
     64   1.1  perseant {
     65  1.26  dholland 	union lfs_dinode *dp;
     66   1.7  perseant 	struct uvnode *vp;
     67   1.7  perseant 	struct inoinfo **inpp, *inp;
     68   1.1  perseant 	struct inoinfo **inpend;
     69   1.7  perseant 	struct inodesc curino;
     70  1.26  dholland 	union lfs_dinode dino;
     71   1.7  perseant 	char pathbuf[MAXPATHLEN + 1];
     72  1.26  dholland 	uint16_t mode;
     73  1.26  dholland 	unsigned ii;
     74   1.1  perseant 
     75  1.19  dholland 	switch (statemap[ULFS_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.17  christos 			err(EEXIT, "%s", "");
     81  1.19  dholland 		if (allocdir(ULFS_ROOTINO, ULFS_ROOTINO, 0755) != ULFS_ROOTINO)
     82  1.25  christos 			err(EEXIT, "CANNOT ALLOCATE ROOT INODE");
     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.19  dholland 			freeino(ULFS_ROOTINO);
     89  1.19  dholland 			if (allocdir(ULFS_ROOTINO, ULFS_ROOTINO, 0755) != ULFS_ROOTINO)
     90  1.24  christos 				err(EEXIT, "CANNOT ALLOCATE ROOT INODE");
     91   1.1  perseant 			break;
     92   1.1  perseant 		}
     93   1.1  perseant 		if (reply("CONTINUE") == 0)
     94  1.17  christos 			err(EEXIT, "%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.19  dholland 			freeino(ULFS_ROOTINO);
    102  1.19  dholland 			if (allocdir(ULFS_ROOTINO, ULFS_ROOTINO, 0755) != ULFS_ROOTINO)
    103  1.24  christos 				err(EEXIT, "CANNOT ALLOCATE ROOT INODE");
    104   1.1  perseant 			break;
    105   1.1  perseant 		}
    106   1.1  perseant 		if (reply("FIX") == 0)
    107  1.17  christos 			errx(EEXIT, "%s", "");
    108  1.19  dholland 		vp = vget(fs, ULFS_ROOTINO);
    109   1.7  perseant 		dp = VTOD(vp);
    110  1.26  dholland 		mode = lfs_dino_getmode(fs, dp);
    111  1.26  dholland 		mode &= ~LFS_IFMT;
    112  1.26  dholland 		mode |= LFS_IFDIR;
    113  1.26  dholland 		lfs_dino_setmode(fs, dp, mode);
    114   1.7  perseant 		inodirty(VTOI(vp));
    115   1.1  perseant 		break;
    116   1.1  perseant 
    117   1.1  perseant 	case DSTATE:
    118   1.1  perseant 		break;
    119   1.1  perseant 
    120   1.1  perseant 	default:
    121  1.24  christos 		errx(EEXIT, "BAD STATE %d FOR ROOT INODE", statemap[ULFS_ROOTINO]);
    122   1.1  perseant 	}
    123  1.19  dholland 	statemap[ULFS_WINO] = FSTATE;
    124  1.21  dholland 	typemap[ULFS_WINO] = LFS_DT_WHT;
    125   1.1  perseant 	/*
    126   1.1  perseant 	 * Sort the directory list into disk block order.
    127   1.1  perseant 	 */
    128   1.7  perseant 	qsort((char *) inpsort, (size_t) inplast, sizeof *inpsort, blksort);
    129   1.1  perseant 	/*
    130   1.1  perseant 	 * Check the integrity of each directory.
    131   1.1  perseant 	 */
    132   1.1  perseant 	memset(&curino, 0, sizeof(struct inodesc));
    133   1.1  perseant 	curino.id_type = DATA;
    134   1.1  perseant 	curino.id_func = pass2check;
    135   1.1  perseant 	inpend = &inpsort[inplast];
    136   1.1  perseant 	for (inpp = inpsort; inpp < inpend; inpp++) {
    137   1.1  perseant 		inp = *inpp;
    138   1.1  perseant 		if (inp->i_isize == 0)
    139   1.1  perseant 			continue;
    140  1.34  dholland 		if (inp->i_isize < MINDIRSIZE(fs)) {
    141   1.1  perseant 			direrror(inp->i_number, "DIRECTORY TOO SHORT");
    142  1.34  dholland 			inp->i_isize = roundup(MINDIRSIZE(fs), LFS_DIRBLKSIZ);
    143   1.1  perseant 			if (reply("FIX") == 1) {
    144   1.7  perseant 				vp = vget(fs, inp->i_number);
    145   1.7  perseant 				dp = VTOD(vp);
    146  1.26  dholland 				lfs_dino_setsize(fs, dp, inp->i_isize);
    147   1.7  perseant 				inodirty(VTOI(vp));
    148   1.1  perseant 			}
    149  1.22  dholland 		} else if ((inp->i_isize & (LFS_DIRBLKSIZ - 1)) != 0) {
    150   1.9    itojun 			getpathname(pathbuf, sizeof(pathbuf), inp->i_number,
    151   1.9    itojun 			    inp->i_number);
    152   1.2   nathanw 			pwarn("DIRECTORY %s: LENGTH %lu NOT MULTIPLE OF %d",
    153  1.22  dholland 			    pathbuf, (unsigned long) inp->i_isize, LFS_DIRBLKSIZ);
    154   1.1  perseant 			if (preen)
    155   1.1  perseant 				printf(" (ADJUSTED)\n");
    156  1.22  dholland 			inp->i_isize = roundup(inp->i_isize, LFS_DIRBLKSIZ);
    157   1.1  perseant 			if (preen || reply("ADJUST") == 1) {
    158   1.7  perseant 				vp = vget(fs, inp->i_number);
    159   1.7  perseant 				dp = VTOD(vp);
    160  1.26  dholland 				lfs_dino_setsize(fs, dp, inp->i_isize);
    161   1.7  perseant 				inodirty(VTOI(vp));
    162   1.1  perseant 			}
    163   1.1  perseant 		}
    164  1.26  dholland 		memset(&dino, 0, sizeof(dino));
    165  1.26  dholland 		lfs_dino_setmode(fs, &dino, LFS_IFDIR);
    166  1.26  dholland 		lfs_dino_setsize(fs, &dino, inp->i_isize);
    167  1.26  dholland 		for (ii = 0; ii < inp->i_numblks / sizeof(inp->i_blks[0]) &&
    168  1.26  dholland 			     ii < ULFS_NDADDR; ii++) {
    169  1.26  dholland 			lfs_dino_setdb(fs, &dino, ii, inp->i_blks[ii]);
    170  1.26  dholland 		}
    171  1.26  dholland 		for (; ii < inp->i_numblks / sizeof(inp->i_blks[0]); ii++) {
    172  1.26  dholland 			lfs_dino_setib(fs, &dino, ii - ULFS_NDADDR,
    173  1.26  dholland 				       inp->i_blks[ii]);
    174  1.26  dholland 		}
    175   1.1  perseant 		curino.id_number = inp->i_number;
    176   1.1  perseant 		curino.id_parent = inp->i_parent;
    177   1.7  perseant 		(void) ckinode(&dino, &curino);
    178   1.1  perseant 	}
    179   1.1  perseant 	/*
    180   1.1  perseant 	 * Now that the parents of all directories have been found,
    181   1.1  perseant 	 * make another pass to verify the value of `..'
    182   1.1  perseant 	 */
    183   1.1  perseant 	for (inpp = inpsort; inpp < inpend; inpp++) {
    184   1.1  perseant 		inp = *inpp;
    185   1.1  perseant 		if (inp->i_parent == 0 || inp->i_isize == 0)
    186   1.1  perseant 			continue;
    187   1.1  perseant 		if (inp->i_dotdot == inp->i_parent ||
    188   1.7  perseant 		    inp->i_dotdot == (ino_t) - 1)
    189   1.1  perseant 			continue;
    190   1.1  perseant 		if (inp->i_dotdot == 0) {
    191   1.1  perseant 			inp->i_dotdot = inp->i_parent;
    192   1.1  perseant 			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
    193   1.1  perseant 			if (reply("FIX") == 0)
    194   1.1  perseant 				continue;
    195   1.7  perseant 			(void) makeentry(inp->i_number, inp->i_parent, "..");
    196   1.1  perseant 			lncntp[inp->i_parent]--;
    197   1.1  perseant 			continue;
    198   1.1  perseant 		}
    199   1.1  perseant 		fileerror(inp->i_parent, inp->i_number,
    200   1.7  perseant 		    "BAD INODE NUMBER FOR '..'");
    201   1.1  perseant 		if (reply("FIX") == 0)
    202   1.1  perseant 			continue;
    203   1.1  perseant 		lncntp[inp->i_dotdot]++;
    204   1.1  perseant 		lncntp[inp->i_parent]--;
    205   1.1  perseant 		inp->i_dotdot = inp->i_parent;
    206   1.7  perseant 		(void) changeino(inp->i_number, "..", inp->i_parent);
    207   1.1  perseant 	}
    208   1.1  perseant 	/*
    209   1.1  perseant 	 * Mark all the directories that can be found from the root.
    210   1.1  perseant 	 */
    211   1.1  perseant 	propagate();
    212   1.1  perseant }
    213   1.1  perseant 
    214   1.1  perseant static int
    215   1.4  perseant pass2check(struct inodesc * idesc)
    216   1.1  perseant {
    217  1.34  dholland 	LFS_DIRHEADER *dirp = idesc->id_dirp;
    218  1.12     perry 	struct inoinfo *inp;
    219   1.7  perseant 	int n, entrysize, ret = 0;
    220  1.26  dholland 	union lfs_dinode *dp;
    221  1.14  christos 	const char *errmsg;
    222  1.34  dholland 	LFS_DIRHEADER proto;
    223   1.7  perseant 	char namebuf[MAXPATHLEN + 1];
    224   1.7  perseant 	char pathbuf[MAXPATHLEN + 1];
    225   1.1  perseant 
    226   1.1  perseant 	/*
    227   1.1  perseant 	 * check for "."
    228   1.1  perseant 	 */
    229   1.1  perseant 	if (idesc->id_entryno != 0)
    230   1.1  perseant 		goto chk1;
    231  1.31  dholland 	if (lfs_dir_getino(fs, dirp) != 0 && strcmp(lfs_dir_nameptr(fs, dirp), ".") == 0) {
    232  1.28  dholland 		if (lfs_dir_getino(fs, dirp) != idesc->id_number) {
    233   1.1  perseant 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
    234  1.27  dholland 			if (reply("FIX") == 1) {
    235  1.28  dholland 				lfs_dir_setino(fs, dirp, idesc->id_number);
    236   1.1  perseant 				ret |= ALTERED;
    237  1.27  dholland 			}
    238   1.1  perseant 		}
    239  1.27  dholland 		if (lfs_dir_gettype(fs, dirp) != LFS_DT_DIR) {
    240   1.1  perseant 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
    241  1.27  dholland 			if (reply("FIX") == 1) {
    242  1.27  dholland 				lfs_dir_settype(fs, dirp, LFS_DT_DIR);
    243   1.1  perseant 				ret |= ALTERED;
    244  1.27  dholland 			}
    245   1.1  perseant 		}
    246   1.1  perseant 		goto chk1;
    247   1.1  perseant 	}
    248   1.1  perseant 	direrror(idesc->id_number, "MISSING '.'");
    249  1.28  dholland 	lfs_dir_setino(fs, &proto, idesc->id_number);
    250  1.27  dholland 	lfs_dir_settype(fs, &proto, LFS_DT_DIR);
    251  1.27  dholland 	lfs_dir_setnamlen(fs, &proto, 1);
    252  1.33  dholland 	entrysize = LFS_DIRECTSIZ(fs, 1);
    253  1.29  dholland 	lfs_dir_setreclen(fs, &proto, entrysize);
    254  1.31  dholland 	if (lfs_dir_getino(fs, dirp) != 0 && strcmp(lfs_dir_nameptr(fs, dirp), "..") != 0) {
    255   1.1  perseant 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
    256  1.31  dholland 		    lfs_dir_nameptr(fs, dirp));
    257  1.28  dholland 	} else if (lfs_dir_getreclen(fs, dirp) < entrysize) {
    258   1.1  perseant 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
    259  1.28  dholland 	} else if (lfs_dir_getreclen(fs, dirp) < 2 * entrysize) {
    260  1.29  dholland 		/* convert this entry to a . entry */
    261  1.28  dholland 		lfs_dir_setreclen(fs, &proto, lfs_dir_getreclen(fs, dirp));
    262  1.29  dholland 		memcpy(dirp, &proto, sizeof(proto));
    263  1.31  dholland 		lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), ".", 1,
    264  1.30  dholland 				lfs_dir_getreclen(fs, dirp));
    265   1.1  perseant 		if (reply("FIX") == 1)
    266   1.1  perseant 			ret |= ALTERED;
    267   1.1  perseant 	} else {
    268  1.29  dholland 		/* split this entry and use the beginning for the . entry */
    269  1.28  dholland 		n = lfs_dir_getreclen(fs, dirp) - entrysize;
    270  1.29  dholland 		memcpy(dirp, &proto, sizeof(proto));
    271  1.31  dholland 		lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), ".", 1,
    272  1.30  dholland 				lfs_dir_getreclen(fs, dirp));
    273   1.1  perseant 		idesc->id_entryno++;
    274  1.28  dholland 		lncntp[lfs_dir_getino(fs, dirp)]--;
    275  1.29  dholland 		dirp = LFS_NEXTDIR(fs, dirp);
    276   1.7  perseant 		memset(dirp, 0, (size_t) n);
    277  1.28  dholland 		lfs_dir_setreclen(fs, dirp, n);
    278   1.1  perseant 		if (reply("FIX") == 1)
    279   1.1  perseant 			ret |= ALTERED;
    280   1.1  perseant 	}
    281   1.1  perseant chk1:
    282   1.1  perseant 	if (idesc->id_entryno > 1)
    283   1.1  perseant 		goto chk2;
    284   1.1  perseant 	inp = getinoinfo(idesc->id_number);
    285  1.28  dholland 	lfs_dir_setino(fs, &proto, inp->i_parent);
    286  1.27  dholland 	lfs_dir_settype(fs, &proto, LFS_DT_DIR);
    287  1.27  dholland 	lfs_dir_setnamlen(fs, &proto, 2);
    288  1.33  dholland 	entrysize = LFS_DIRECTSIZ(fs, 2);
    289  1.29  dholland 	lfs_dir_setreclen(fs, &proto, entrysize);
    290   1.1  perseant 	if (idesc->id_entryno == 0) {
    291  1.27  dholland 		n = LFS_DIRSIZ(fs, dirp);
    292  1.28  dholland 		if (lfs_dir_getreclen(fs, dirp) < n + entrysize)
    293   1.1  perseant 			goto chk2;
    294  1.28  dholland 		lfs_dir_setreclen(fs, &proto, lfs_dir_getreclen(fs, dirp) - n);
    295  1.28  dholland 		lfs_dir_setreclen(fs, dirp, n);
    296   1.1  perseant 		idesc->id_entryno++;
    297  1.28  dholland 		lncntp[lfs_dir_getino(fs, dirp)]--;
    298  1.34  dholland 		dirp = (LFS_DIRHEADER *) ((char *) (dirp) + n);
    299  1.28  dholland 		memset(dirp, 0, lfs_dir_getreclen(fs, &proto));
    300  1.28  dholland 		lfs_dir_setreclen(fs, dirp, lfs_dir_getreclen(fs, &proto));
    301   1.1  perseant 	}
    302  1.31  dholland 	if (lfs_dir_getino(fs, dirp) != 0 && strcmp(lfs_dir_nameptr(fs, dirp), "..") == 0) {
    303  1.28  dholland 		inp->i_dotdot = lfs_dir_getino(fs, dirp);
    304  1.27  dholland 		if (lfs_dir_gettype(fs, dirp) != LFS_DT_DIR) {
    305   1.1  perseant 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
    306  1.27  dholland 			lfs_dir_settype(fs, dirp, LFS_DT_DIR);
    307   1.1  perseant 			if (reply("FIX") == 1)
    308   1.1  perseant 				ret |= ALTERED;
    309   1.1  perseant 		}
    310   1.1  perseant 		goto chk2;
    311   1.1  perseant 	}
    312  1.31  dholland 	if (lfs_dir_getino(fs, dirp) != 0 && strcmp(lfs_dir_nameptr(fs, dirp), ".") != 0) {
    313   1.1  perseant 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    314   1.1  perseant 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
    315  1.31  dholland 		    lfs_dir_nameptr(fs, dirp));
    316   1.7  perseant 		inp->i_dotdot = (ino_t) - 1;
    317  1.28  dholland 	} else if (lfs_dir_getreclen(fs, dirp) < entrysize) {
    318   1.1  perseant 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    319   1.1  perseant 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
    320   1.7  perseant 		inp->i_dotdot = (ino_t) - 1;
    321   1.1  perseant 	} else if (inp->i_parent != 0) {
    322   1.1  perseant 		/*
    323   1.1  perseant 		 * We know the parent, so fix now.
    324   1.1  perseant 		 */
    325   1.1  perseant 		inp->i_dotdot = inp->i_parent;
    326   1.1  perseant 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    327  1.28  dholland 		lfs_dir_setreclen(fs, &proto, lfs_dir_getreclen(fs, dirp));
    328   1.7  perseant 		memcpy(dirp, &proto, (size_t) entrysize);
    329  1.31  dholland 		lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), "..", 2,
    330  1.30  dholland 				lfs_dir_getreclen(fs, dirp));
    331   1.1  perseant 		if (reply("FIX") == 1)
    332   1.1  perseant 			ret |= ALTERED;
    333   1.1  perseant 	}
    334   1.1  perseant 	idesc->id_entryno++;
    335  1.28  dholland 	if (lfs_dir_getino(fs, dirp) != 0)
    336  1.28  dholland 		lncntp[lfs_dir_getino(fs, dirp)]--;
    337   1.4  perseant 	return (ret | KEEPON);
    338   1.1  perseant chk2:
    339  1.28  dholland 	if (lfs_dir_getino(fs, dirp) == 0)
    340   1.4  perseant 		return (ret | KEEPON);
    341  1.27  dholland 	if (lfs_dir_getnamlen(fs, dirp) <= 2 &&
    342  1.31  dholland 	    lfs_dir_nameptr(fs, dirp)[0] == '.' &&
    343   1.1  perseant 	    idesc->id_entryno >= 2) {
    344  1.27  dholland 		if (lfs_dir_getnamlen(fs, dirp) == 1) {
    345   1.1  perseant 			direrror(idesc->id_number, "EXTRA '.' ENTRY");
    346  1.27  dholland 			if (reply("FIX") == 1) {
    347  1.28  dholland 				lfs_dir_setino(fs, dirp, 0);
    348   1.1  perseant 				ret |= ALTERED;
    349  1.27  dholland 			}
    350   1.1  perseant 			return (KEEPON | ret);
    351   1.1  perseant 		}
    352  1.31  dholland 		if (lfs_dir_nameptr(fs, dirp)[1] == '.') {
    353   1.1  perseant 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
    354  1.27  dholland 			if (reply("FIX") == 1) {
    355  1.28  dholland 				lfs_dir_setino(fs, dirp, 0);
    356   1.1  perseant 				ret |= ALTERED;
    357  1.27  dholland 			}
    358   1.1  perseant 			return (KEEPON | ret);
    359   1.1  perseant 		}
    360   1.1  perseant 	}
    361   1.1  perseant 	idesc->id_entryno++;
    362   1.1  perseant 	n = 0;
    363  1.28  dholland 	if (lfs_dir_getino(fs, dirp) >= maxino) {
    364  1.28  dholland 		fileerror(idesc->id_number, lfs_dir_getino(fs, dirp), "I OUT OF RANGE");
    365   1.1  perseant 		n = reply("REMOVE");
    366  1.28  dholland 	} else if (lfs_dir_getino(fs, dirp) == LFS_IFILE_INUM &&
    367  1.19  dholland 	    idesc->id_number == ULFS_ROOTINO) {
    368  1.27  dholland 		if (lfs_dir_gettype(fs, dirp) != LFS_DT_REG) {
    369  1.28  dholland 			fileerror(idesc->id_number, lfs_dir_getino(fs, dirp),
    370   1.7  perseant 			    "BAD TYPE FOR IFILE");
    371  1.27  dholland 			if (reply("FIX") == 1) {
    372  1.27  dholland 				lfs_dir_settype(fs, dirp, LFS_DT_REG);
    373   1.4  perseant 				ret |= ALTERED;
    374  1.27  dholland 			}
    375   1.4  perseant 		}
    376  1.28  dholland 	} else if (((lfs_dir_getino(fs, dirp) == ULFS_WINO && lfs_dir_gettype(fs, dirp) != LFS_DT_WHT) ||
    377  1.28  dholland 		(lfs_dir_getino(fs, dirp) != ULFS_WINO && lfs_dir_gettype(fs, dirp) == LFS_DT_WHT))) {
    378  1.28  dholland 		fileerror(idesc->id_number, lfs_dir_getino(fs, dirp), "BAD WHITEOUT ENTRY");
    379  1.27  dholland 		if (reply("FIX") == 1) {
    380  1.28  dholland 			lfs_dir_setino(fs, dirp, ULFS_WINO);
    381  1.27  dholland 			lfs_dir_settype(fs, dirp, LFS_DT_WHT);
    382   1.1  perseant 			ret |= ALTERED;
    383  1.27  dholland 		}
    384   1.1  perseant 	} else {
    385   1.1  perseant again:
    386  1.28  dholland 		switch (statemap[lfs_dir_getino(fs, dirp)]) {
    387   1.1  perseant 		case USTATE:
    388   1.1  perseant 			if (idesc->id_entryno <= 2)
    389   1.1  perseant 				break;
    390  1.28  dholland 			fileerror(idesc->id_number, lfs_dir_getino(fs, dirp),
    391  1.28  dholland 			    "UNALLOCATED");
    392   1.1  perseant 			n = reply("REMOVE");
    393   1.1  perseant 			break;
    394   1.1  perseant 
    395   1.1  perseant 		case DCLEAR:
    396   1.1  perseant 		case FCLEAR:
    397   1.1  perseant 			if (idesc->id_entryno <= 2)
    398   1.1  perseant 				break;
    399  1.28  dholland 			if (statemap[lfs_dir_getino(fs, dirp)] == FCLEAR)
    400   1.1  perseant 				errmsg = "DUP/BAD";
    401   1.1  perseant 			else if (!preen)
    402   1.1  perseant 				errmsg = "ZERO LENGTH DIRECTORY";
    403   1.1  perseant 			else {
    404   1.1  perseant 				n = 1;
    405   1.1  perseant 				break;
    406   1.1  perseant 			}
    407  1.28  dholland 			fileerror(idesc->id_number, lfs_dir_getino(fs, dirp), errmsg);
    408   1.1  perseant 			if ((n = reply("REMOVE")) == 1)
    409   1.1  perseant 				break;
    410  1.28  dholland 			dp = ginode(lfs_dir_getino(fs, dirp));
    411  1.28  dholland 			statemap[lfs_dir_getino(fs, dirp)] =
    412  1.26  dholland 			    (lfs_dino_getmode(fs, dp) & LFS_IFMT) == LFS_IFDIR ? DSTATE : FSTATE;
    413  1.28  dholland 			lncntp[lfs_dir_getino(fs, dirp)] = lfs_dino_getnlink(fs, dp);
    414   1.1  perseant 			goto again;
    415   1.1  perseant 
    416   1.1  perseant 		case DSTATE:
    417   1.1  perseant 		case DFOUND:
    418  1.28  dholland 			inp = getinoinfo(lfs_dir_getino(fs, dirp));
    419   1.1  perseant 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
    420   1.9    itojun 				getpathname(pathbuf, sizeof(pathbuf),
    421   1.9    itojun 				    idesc->id_number, idesc->id_number);
    422   1.9    itojun 				getpathname(namebuf, sizeof(namebuf),
    423  1.28  dholland 				    lfs_dir_getino(fs, dirp),
    424  1.28  dholland 				    lfs_dir_getino(fs, dirp));
    425   1.1  perseant 				pwarn("%s %s %s\n", pathbuf,
    426   1.7  perseant 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
    427   1.7  perseant 				    namebuf);
    428   1.1  perseant 				if (preen)
    429   1.1  perseant 					printf(" (IGNORED)\n");
    430   1.1  perseant 				else if ((n = reply("REMOVE")) == 1)
    431   1.1  perseant 					break;
    432   1.1  perseant 			}
    433   1.1  perseant 			if (idesc->id_entryno > 2)
    434   1.1  perseant 				inp->i_parent = idesc->id_number;
    435   1.1  perseant 			/* fall through */
    436   1.1  perseant 
    437   1.1  perseant 		case FSTATE:
    438  1.28  dholland 			if (lfs_dir_gettype(fs, dirp) != typemap[lfs_dir_getino(fs, dirp)]) {
    439  1.28  dholland 				fileerror(idesc->id_number,
    440  1.28  dholland 				    lfs_dir_getino(fs, dirp),
    441   1.7  perseant 				    "BAD TYPE VALUE");
    442  1.13  perseant 				if (debug)
    443  1.13  perseant 					pwarn("dir has %d, typemap has %d\n",
    444  1.28  dholland 						lfs_dir_gettype(fs, dirp), typemap[lfs_dir_getino(fs, dirp)]);
    445  1.28  dholland 				lfs_dir_settype(fs, dirp, typemap[lfs_dir_getino(fs, dirp)]);
    446   1.1  perseant 				if (reply("FIX") == 1)
    447   1.1  perseant 					ret |= ALTERED;
    448   1.1  perseant 			}
    449  1.28  dholland 			lncntp[lfs_dir_getino(fs, dirp)]--;
    450   1.1  perseant 			break;
    451   1.1  perseant 
    452   1.1  perseant 		default:
    453  1.28  dholland 			errx(EEXIT, "BAD STATE %d FOR INODE I=%ju",
    454  1.28  dholland 			    statemap[lfs_dir_getino(fs, dirp)],
    455  1.28  dholland 			    (uintmax_t)lfs_dir_getino(fs, dirp));
    456   1.1  perseant 		}
    457   1.1  perseant 	}
    458   1.1  perseant 	if (n == 0)
    459   1.4  perseant 		return (ret | KEEPON);
    460  1.28  dholland 	lfs_dir_setino(fs, dirp, 0);
    461   1.4  perseant 	return (ret | KEEPON | ALTERED);
    462   1.1  perseant }
    463   1.1  perseant /*
    464   1.1  perseant  * Routine to sort disk blocks.
    465   1.1  perseant  */
    466   1.1  perseant static int
    467   1.4  perseant blksort(const void *inpp1, const void *inpp2)
    468   1.1  perseant {
    469  1.14  christos 	return ((*(const struct inoinfo *const *) inpp1)->i_blks[0] -
    470  1.14  christos 	    (*(const struct inoinfo *const *) inpp2)->i_blks[0]);
    471   1.1  perseant }
    472