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