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