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