Home | History | Annotate | Line # | Download | only in fsck_ffs
pass2.c revision 1.47
      1  1.47  christos /*	$NetBSD: pass2.c,v 1.47 2011/06/09 19:57:52 christos 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.36       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.18     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.12       cgd #if 0
     35  1.20     lukem static char sccsid[] = "@(#)pass2.c	8.9 (Berkeley) 4/28/95";
     36  1.12       cgd #else
     37  1.47  christos __RCSID("$NetBSD: pass2.c,v 1.47 2011/06/09 19:57:52 christos Exp $");
     38  1.12       cgd #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #include <sys/param.h>
     42   1.5       cgd #include <sys/time.h>
     43  1.20     lukem 
     44   1.6   mycroft #include <ufs/ufs/dinode.h>
     45   1.6   mycroft #include <ufs/ufs/dir.h>
     46   1.6   mycroft #include <ufs/ffs/fs.h>
     47   1.9       cgd 
     48  1.20     lukem #include <err.h>
     49  1.21     lukem #include <stdio.h>
     50  1.21     lukem #include <stdlib.h>
     51   1.1       cgd #include <string.h>
     52  1.16  christos 
     53   1.1       cgd #include "fsck.h"
     54  1.17  christos #include "fsutil.h"
     55   1.9       cgd #include "extern.h"
     56  1.45  christos #include "exitvalues.h"
     57   1.1       cgd 
     58   1.1       cgd #define MINDIRSIZE	(sizeof (struct dirtemplate))
     59   1.1       cgd 
     60  1.41   xtraeme static int blksort(const void *, const void *);
     61  1.41   xtraeme static int pass2check(struct inodesc *);
     62   1.1       cgd 
     63   1.9       cgd void
     64  1.40   xtraeme pass2(void)
     65   1.1       cgd {
     66  1.33      fvdl 	union dinode *dp;
     67  1.29   mycroft 	struct inoinfo **inpp, *inp, *pinp;
     68   1.1       cgd 	struct inoinfo **inpend;
     69  1.33      fvdl 	struct inostat *rinfo, *info;
     70   1.1       cgd 	struct inodesc curino;
     71  1.33      fvdl 	union dinode dino;
     72  1.33      fvdl 	int i, maxblk;
     73   1.1       cgd 	char pathbuf[MAXPATHLEN + 1];
     74   1.1       cgd 
     75  1.33      fvdl 	rinfo = inoinfo(ROOTINO);
     76  1.33      fvdl 	switch (rinfo->ino_state) {
     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.47  christos 			ckfini(1);
     83  1.45  christos 			exit(FSCK_EXIT_CHECK_FAILED);
     84  1.26      fvdl 		}
     85   1.1       cgd 		if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
     86  1.45  christos 			errexit("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.45  christos 				errexit("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.47  christos 			ckfini(1);
    100  1.45  christos 			exit(FSCK_EXIT_CHECK_FAILED);
    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.45  christos 				errexit("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.47  christos 			ckfini(1);
    116  1.45  christos 			exit(FSCK_EXIT_CHECK_FAILED);
    117  1.26      fvdl 		}
    118   1.1       cgd 		dp = ginode(ROOTINO);
    119  1.43     skrll 		DIP_SET(dp, mode,
    120  1.43     skrll 		    iswap16((iswap16(DIP(dp, mode)) & ~IFMT) | IFDIR));
    121   1.1       cgd 		inodirty();
    122   1.1       cgd 		break;
    123   1.1       cgd 
    124   1.1       cgd 	case DSTATE:
    125   1.1       cgd 		break;
    126   1.1       cgd 
    127   1.1       cgd 	default:
    128  1.45  christos 		errexit("BAD STATE %d FOR ROOT INODE", rinfo->ino_state);
    129   1.1       cgd 	}
    130  1.10   mycroft 	if (newinofmt) {
    131  1.33      fvdl 		info = inoinfo(WINO);
    132  1.33      fvdl 		info->ino_state = FSTATE;
    133  1.33      fvdl 		info->ino_type = DT_WHT;
    134  1.10   mycroft 	}
    135   1.1       cgd 	/*
    136   1.1       cgd 	 * Sort the directory list into disk block order.
    137   1.1       cgd 	 */
    138   1.1       cgd 	qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
    139   1.1       cgd 	/*
    140   1.1       cgd 	 * Check the integrity of each directory.
    141   1.1       cgd 	 */
    142   1.7   mycroft 	memset(&curino, 0, sizeof(struct inodesc));
    143   1.1       cgd 	curino.id_type = DATA;
    144   1.1       cgd 	curino.id_func = pass2check;
    145   1.1       cgd 	inpend = &inpsort[inplast];
    146   1.1       cgd 	for (inpp = inpsort; inpp < inpend; inpp++) {
    147  1.30     lukem 		if (got_siginfo) {
    148  1.30     lukem 			fprintf(stderr,
    149  1.31       agc 			    "%s: phase 2: dir %ld of %d (%d%%)\n", cdevname(),
    150  1.31       agc 			    (long)(inpp - inpsort), (int)inplast,
    151  1.30     lukem 			    (int)((inpp - inpsort) * 100 / inplast));
    152  1.30     lukem 			got_siginfo = 0;
    153  1.30     lukem 		}
    154  1.39  christos #ifdef PROGRESS
    155  1.38  christos 		progress_bar(cdevname(), preen ? NULL : "phase 2",
    156  1.38  christos 			    (inpp - inpsort), inplast);
    157  1.39  christos #endif /* PROGRESS */
    158   1.1       cgd 		inp = *inpp;
    159   1.1       cgd 		if (inp->i_isize == 0)
    160   1.1       cgd 			continue;
    161   1.1       cgd 		if (inp->i_isize < MINDIRSIZE) {
    162   1.1       cgd 			direrror(inp->i_number, "DIRECTORY TOO SHORT");
    163  1.32       dbj 			inp->i_isize = roundup(MINDIRSIZE, dirblksiz);
    164   1.1       cgd 			if (reply("FIX") == 1) {
    165   1.1       cgd 				dp = ginode(inp->i_number);
    166  1.43     skrll 				DIP_SET(dp, size, iswap64(inp->i_isize));
    167   1.1       cgd 				inodirty();
    168  1.24    bouyer 			} else
    169  1.24    bouyer 				markclean = 0;
    170  1.32       dbj 		} else if ((inp->i_isize & (dirblksiz - 1)) != 0) {
    171  1.35    itojun 			getpathname(pathbuf, sizeof(pathbuf), inp->i_number,
    172  1.35    itojun 			    inp->i_number);
    173  1.26      fvdl 			if (usedsoftdep)
    174  1.28     lukem 				pfatal("%s %s: LENGTH %lld NOT MULTIPLE OF %d",
    175  1.27       mrg 					"DIRECTORY", pathbuf,
    176  1.32       dbj 					(long long)inp->i_isize, dirblksiz);
    177  1.26      fvdl 			else
    178  1.28     lukem 				pwarn("%s %s: LENGTH %lld NOT MULTIPLE OF %d",
    179  1.27       mrg 					"DIRECTORY", pathbuf,
    180  1.32       dbj 					(long long)inp->i_isize, dirblksiz);
    181   1.1       cgd 			if (preen)
    182   1.1       cgd 				printf(" (ADJUSTED)\n");
    183  1.32       dbj 			inp->i_isize = roundup(inp->i_isize, dirblksiz);
    184   1.1       cgd 			if (preen || reply("ADJUST") == 1) {
    185   1.1       cgd 				dp = ginode(inp->i_number);
    186  1.43     skrll 				DIP_SET(dp, size, iswap64(inp->i_isize));
    187   1.1       cgd 				inodirty();
    188  1.24    bouyer 			} else
    189  1.24    bouyer 				markclean = 0;
    190  1.34      fvdl 		}
    191  1.33      fvdl 		memset(&dino, 0, sizeof dino);
    192  1.33      fvdl 		dp = &dino;
    193  1.33      fvdl 		if (!is_ufs2) {
    194  1.33      fvdl 			dp->dp1.di_mode = iswap16(IFDIR);
    195  1.33      fvdl 			dp->dp1.di_size = iswap64(inp->i_isize);
    196  1.33      fvdl 			maxblk = inp->i_numblks < NDADDR ? inp->i_numblks :
    197  1.33      fvdl 			    NDADDR;
    198  1.33      fvdl 			for (i = 0; i < maxblk; i++)
    199  1.33      fvdl 				dp->dp1.di_db[i] = inp->i_blks[i];
    200  1.34      fvdl 			if (inp->i_numblks > NDADDR) {
    201  1.34      fvdl 				for (i = 0; i < NIADDR; i++)
    202  1.34      fvdl 					dp->dp1.di_ib[i] =
    203  1.34      fvdl 					    inp->i_blks[NDADDR + i];
    204  1.34      fvdl 			}
    205  1.33      fvdl 		} else {
    206  1.33      fvdl 			dp->dp2.di_mode = iswap16(IFDIR);
    207  1.33      fvdl 			dp->dp2.di_size = iswap64(inp->i_isize);
    208  1.33      fvdl 			maxblk = inp->i_numblks < NDADDR ? inp->i_numblks :
    209  1.33      fvdl 			    NDADDR;
    210  1.33      fvdl 			for (i = 0; i < maxblk; i++)
    211  1.33      fvdl 				dp->dp2.di_db[i] = inp->i_blks[i];
    212  1.34      fvdl 			if (inp->i_numblks > NDADDR) {
    213  1.34      fvdl 				for (i = 0; i < NIADDR; i++)
    214  1.34      fvdl 					dp->dp2.di_ib[i] =
    215  1.34      fvdl 					    inp->i_blks[NDADDR + i];
    216  1.34      fvdl 			}
    217  1.33      fvdl 		}
    218  1.24    bouyer 		curino.id_number = inp->i_number;
    219  1.24    bouyer 		curino.id_parent = inp->i_parent;
    220  1.46    bouyer 		curino.id_uid = iswap32(DIP(dp, uid));
    221  1.46    bouyer 		curino.id_gid = iswap32(DIP(dp, gid));
    222  1.24    bouyer 		(void)ckinode(&dino, &curino);
    223  1.33      fvdl 	}
    224  1.24    bouyer 
    225  1.33      fvdl 	/*
    226  1.33      fvdl 	 * Byte swapping in directory entries, if needed, has been done.
    227  1.24    bouyer 	 * Now rescan dirs for pass2check()
    228  1.24    bouyer 	 */
    229  1.24    bouyer 	if (do_dirswap) {
    230  1.24    bouyer 		do_dirswap = 0;
    231  1.24    bouyer 		for (inpp = inpsort; inpp < inpend; inpp++) {
    232  1.24    bouyer 			inp = *inpp;
    233  1.24    bouyer 			if (inp->i_isize == 0)
    234  1.24    bouyer 				continue;
    235  1.33      fvdl 			memset(&dino, 0, sizeof dino);
    236  1.33      fvdl 			if (!is_ufs2) {
    237  1.33      fvdl 				dino.dp1.di_mode = iswap16(IFDIR);
    238  1.33      fvdl 				dino.dp1.di_size = iswap64(inp->i_isize);
    239  1.33      fvdl 				for (i = 0; i < inp->i_numblks; i++)
    240  1.33      fvdl 					dino.dp1.di_db[i] = inp->i_blks[i];
    241  1.33      fvdl 			} else {
    242  1.33      fvdl 				dino.dp2.di_mode = iswap16(IFDIR);
    243  1.33      fvdl 				dino.dp2.di_size = iswap64(inp->i_isize);
    244  1.33      fvdl 				for (i = 0; i < inp->i_numblks; i++)
    245  1.33      fvdl 					dino.dp2.di_db[i] = inp->i_blks[i];
    246  1.33      fvdl 			}
    247  1.33      fvdl 			curino.id_number = inp->i_number;
    248  1.33      fvdl 			curino.id_parent = inp->i_parent;
    249  1.46    bouyer 			curino.id_uid = iswap32(DIP(&dino, uid));
    250  1.46    bouyer 			curino.id_gid = iswap32(DIP(&dino, gid));
    251  1.33      fvdl 			(void)ckinode(&dino, &curino);
    252  1.33      fvdl 		}
    253  1.24    bouyer 	}
    254  1.24    bouyer 
    255   1.1       cgd 	/*
    256   1.1       cgd 	 * Now that the parents of all directories have been found,
    257   1.1       cgd 	 * make another pass to verify the value of `..'
    258   1.1       cgd 	 */
    259   1.1       cgd 	for (inpp = inpsort; inpp < inpend; inpp++) {
    260   1.1       cgd 		inp = *inpp;
    261   1.1       cgd 		if (inp->i_parent == 0 || inp->i_isize == 0)
    262   1.1       cgd 			continue;
    263   1.1       cgd 		if (inp->i_dotdot == inp->i_parent ||
    264   1.1       cgd 		    inp->i_dotdot == (ino_t)-1)
    265   1.1       cgd 			continue;
    266  1.33      fvdl 		info = inoinfo(inp->i_parent);
    267   1.1       cgd 		if (inp->i_dotdot == 0) {
    268   1.1       cgd 			inp->i_dotdot = inp->i_parent;
    269   1.1       cgd 			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
    270  1.24    bouyer 			if (reply("FIX") == 0) {
    271  1.24    bouyer 				markclean = 0;
    272   1.1       cgd 				continue;
    273  1.24    bouyer 			}
    274   1.1       cgd 			(void)makeentry(inp->i_number, inp->i_parent, "..");
    275  1.33      fvdl 			info->ino_linkcnt--;
    276   1.1       cgd 			continue;
    277   1.1       cgd 		}
    278   1.1       cgd 		fileerror(inp->i_parent, inp->i_number,
    279   1.6   mycroft 		    "BAD INODE NUMBER FOR '..'");
    280  1.24    bouyer 		if (reply("FIX") == 0) {
    281  1.24    bouyer 			markclean = 0;
    282   1.1       cgd 			continue;
    283  1.24    bouyer 		}
    284  1.33      fvdl 		inoinfo(inp->i_dotdot)->ino_linkcnt++;
    285  1.33      fvdl 		info->ino_linkcnt--;
    286   1.1       cgd 		inp->i_dotdot = inp->i_parent;
    287   1.1       cgd 		(void)changeino(inp->i_number, "..", inp->i_parent);
    288   1.1       cgd 	}
    289   1.1       cgd 	/*
    290  1.29   mycroft 	 * Create a list of children for each directory.
    291  1.29   mycroft 	 */
    292  1.29   mycroft 	inpend = &inpsort[inplast];
    293  1.29   mycroft 	for (inpp = inpsort; inpp < inpend; inpp++) {
    294  1.29   mycroft 		inp = *inpp;
    295  1.33      fvdl 		info = inoinfo(inp->i_number);
    296  1.37   mycroft 		inp->i_child = inp->i_sibling = 0;
    297  1.33      fvdl 		if (info->ino_state == DFOUND)
    298  1.33      fvdl 			info->ino_state = DSTATE;
    299  1.29   mycroft 	}
    300  1.29   mycroft 	for (inpp = inpsort; inpp < inpend; inpp++) {
    301  1.29   mycroft 		inp = *inpp;
    302  1.29   mycroft 		if (inp->i_parent == 0 ||
    303  1.29   mycroft 		    inp->i_number == ROOTINO)
    304  1.29   mycroft 			continue;
    305  1.29   mycroft 		pinp = getinoinfo(inp->i_parent);
    306  1.29   mycroft 		inp->i_sibling = pinp->i_child;
    307  1.29   mycroft 		pinp->i_child = inp;
    308  1.29   mycroft 	}
    309  1.29   mycroft 	/*
    310   1.1       cgd 	 * Mark all the directories that can be found from the root.
    311   1.1       cgd 	 */
    312  1.29   mycroft 	propagate(ROOTINO);
    313  1.38  christos 
    314  1.39  christos #ifdef PROGRESS
    315  1.44       apb 	if (!preen)
    316  1.38  christos 		progress_done();
    317  1.39  christos #endif /* PROGRESS */
    318   1.1       cgd }
    319   1.1       cgd 
    320  1.16  christos static int
    321  1.40   xtraeme pass2check(struct inodesc *idesc)
    322   1.1       cgd {
    323  1.18     lukem 	struct direct *dirp = idesc->id_dirp;
    324  1.18     lukem 	struct inoinfo *inp;
    325  1.33      fvdl 	struct inostat *info;
    326   1.1       cgd 	int n, entrysize, ret = 0;
    327  1.33      fvdl 	union dinode *dp;
    328  1.42  christos 	const char *errmsg;
    329   1.1       cgd 	struct direct proto;
    330   1.1       cgd 	char namebuf[MAXPATHLEN + 1];
    331   1.1       cgd 	char pathbuf[MAXPATHLEN + 1];
    332   1.1       cgd 
    333   1.6   mycroft 	/*
    334   1.6   mycroft 	 * If converting, set directory entry type.
    335   1.6   mycroft 	 */
    336  1.33      fvdl 	if (!is_ufs2 && doinglevel2 && iswap32(dirp->d_ino) > 0 &&
    337  1.33      fvdl 	    iswap32(dirp->d_ino) < maxino) {
    338  1.33      fvdl 		dirp->d_type = inoinfo(iswap32(dirp->d_ino))->ino_type;
    339   1.6   mycroft 		ret |= ALTERED;
    340   1.6   mycroft 	}
    341   1.1       cgd 	/*
    342   1.1       cgd 	 * check for "."
    343   1.1       cgd 	 */
    344   1.1       cgd 	if (idesc->id_entryno != 0)
    345   1.1       cgd 		goto chk1;
    346   1.1       cgd 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
    347  1.24    bouyer 		if (iswap32(dirp->d_ino) != idesc->id_number) {
    348   1.1       cgd 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
    349  1.24    bouyer 			dirp->d_ino = iswap32(idesc->id_number);
    350   1.1       cgd 			if (reply("FIX") == 1)
    351   1.1       cgd 				ret |= ALTERED;
    352  1.24    bouyer 			else
    353  1.24    bouyer 				markclean = 0;
    354   1.1       cgd 		}
    355   1.6   mycroft 		if (newinofmt && dirp->d_type != DT_DIR) {
    356   1.6   mycroft 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
    357   1.6   mycroft 			dirp->d_type = DT_DIR;
    358   1.6   mycroft 			if (reply("FIX") == 1)
    359   1.6   mycroft 				ret |= ALTERED;
    360  1.24    bouyer 			else
    361  1.24    bouyer 				markclean = 0;
    362   1.6   mycroft 		}
    363   1.1       cgd 		goto chk1;
    364   1.1       cgd 	}
    365   1.1       cgd 	direrror(idesc->id_number, "MISSING '.'");
    366  1.24    bouyer 	proto.d_ino = iswap32(idesc->id_number);
    367   1.6   mycroft 	if (newinofmt)
    368   1.6   mycroft 		proto.d_type = DT_DIR;
    369   1.6   mycroft 	else
    370   1.6   mycroft 		proto.d_type = 0;
    371   1.1       cgd 	proto.d_namlen = 1;
    372  1.35    itojun 	(void)strlcpy(proto.d_name, ".", sizeof(proto.d_name));
    373  1.10   mycroft #	if BYTE_ORDER == LITTLE_ENDIAN
    374  1.24    bouyer 		if (!newinofmt && !needswap) {
    375  1.24    bouyer #	else
    376  1.24    bouyer 		if (!newinofmt && needswap) {
    377  1.24    bouyer #	endif
    378  1.10   mycroft 			u_char tmp;
    379  1.10   mycroft 
    380  1.10   mycroft 			tmp = proto.d_type;
    381  1.10   mycroft 			proto.d_type = proto.d_namlen;
    382  1.10   mycroft 			proto.d_namlen = tmp;
    383  1.10   mycroft 		}
    384  1.24    bouyer 	entrysize = DIRSIZ(0, &proto, 0);
    385   1.1       cgd 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
    386   1.1       cgd 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
    387   1.1       cgd 			dirp->d_name);
    388  1.24    bouyer 		markclean = 0;
    389  1.24    bouyer 	} else if (iswap16(dirp->d_reclen) < entrysize) {
    390   1.1       cgd 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
    391  1.24    bouyer 		markclean = 0;
    392  1.24    bouyer 	} else if (iswap16(dirp->d_reclen) < 2 * entrysize) {
    393   1.1       cgd 		proto.d_reclen = dirp->d_reclen;
    394  1.20     lukem 		memmove(dirp, &proto, (size_t)entrysize);
    395   1.1       cgd 		if (reply("FIX") == 1)
    396   1.1       cgd 			ret |= ALTERED;
    397  1.24    bouyer 		else
    398  1.24    bouyer 			markclean = 0;
    399   1.1       cgd 	} else {
    400  1.24    bouyer 		n = iswap16(dirp->d_reclen) - entrysize;
    401  1.24    bouyer 		proto.d_reclen = iswap16(entrysize);
    402  1.20     lukem 		memmove(dirp, &proto, (size_t)entrysize);
    403   1.1       cgd 		idesc->id_entryno++;
    404  1.33      fvdl 		inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
    405   1.1       cgd 		dirp = (struct direct *)((char *)(dirp) + entrysize);
    406   1.7   mycroft 		memset(dirp, 0, (size_t)n);
    407  1.24    bouyer 		dirp->d_reclen = iswap16(n);
    408   1.1       cgd 		if (reply("FIX") == 1)
    409   1.1       cgd 			ret |= ALTERED;
    410  1.24    bouyer 		else
    411  1.24    bouyer 			markclean = 0;
    412   1.1       cgd 	}
    413   1.1       cgd chk1:
    414   1.1       cgd 	if (idesc->id_entryno > 1)
    415   1.1       cgd 		goto chk2;
    416   1.1       cgd 	inp = getinoinfo(idesc->id_number);
    417  1.24    bouyer 	proto.d_ino = iswap32(inp->i_parent);
    418   1.6   mycroft 	if (newinofmt)
    419   1.6   mycroft 		proto.d_type = DT_DIR;
    420   1.6   mycroft 	else
    421   1.6   mycroft 		proto.d_type = 0;
    422   1.1       cgd 	proto.d_namlen = 2;
    423  1.35    itojun 	(void)strlcpy(proto.d_name, "..", sizeof(proto.d_name));
    424  1.35    itojun #if BYTE_ORDER == LITTLE_ENDIAN
    425  1.35    itojun 	if (!newinofmt && !needswap) {
    426  1.35    itojun #else
    427  1.35    itojun 	if (!newinofmt && needswap) {
    428  1.35    itojun #endif
    429  1.35    itojun 		u_char tmp;
    430  1.11   mycroft 
    431  1.35    itojun 		tmp = proto.d_type;
    432  1.35    itojun 		proto.d_type = proto.d_namlen;
    433  1.35    itojun 		proto.d_namlen = tmp;
    434  1.35    itojun 	}
    435  1.24    bouyer 	entrysize = DIRSIZ(0, &proto, 0);
    436   1.1       cgd 	if (idesc->id_entryno == 0) {
    437  1.24    bouyer 		n = DIRSIZ(0, dirp, 0);
    438  1.24    bouyer 		if (iswap16(dirp->d_reclen) < n + entrysize)
    439   1.1       cgd 			goto chk2;
    440  1.24    bouyer 		proto.d_reclen = iswap16(iswap16(dirp->d_reclen) - n);
    441  1.24    bouyer 		dirp->d_reclen = iswap16(n);
    442   1.1       cgd 		idesc->id_entryno++;
    443  1.33      fvdl 		inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
    444   1.1       cgd 		dirp = (struct direct *)((char *)(dirp) + n);
    445  1.24    bouyer 		memset(dirp, 0, (size_t)iswap16(proto.d_reclen));
    446   1.1       cgd 		dirp->d_reclen = proto.d_reclen;
    447   1.1       cgd 	}
    448   1.1       cgd 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
    449  1.24    bouyer 		inp->i_dotdot = iswap32(dirp->d_ino);
    450   1.6   mycroft 		if (newinofmt && dirp->d_type != DT_DIR) {
    451   1.6   mycroft 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
    452   1.6   mycroft 			dirp->d_type = DT_DIR;
    453   1.6   mycroft 			if (reply("FIX") == 1)
    454   1.6   mycroft 				ret |= ALTERED;
    455  1.24    bouyer 			else
    456  1.24    bouyer 				markclean = 0;
    457   1.6   mycroft 		}
    458   1.1       cgd 		goto chk2;
    459   1.1       cgd 	}
    460  1.24    bouyer 	if (iswap32(dirp->d_ino) != 0 && strcmp(dirp->d_name, ".") != 0) {
    461   1.1       cgd 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    462   1.1       cgd 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
    463   1.1       cgd 			dirp->d_name);
    464   1.1       cgd 		inp->i_dotdot = (ino_t)-1;
    465  1.24    bouyer 		markclean = 0;
    466  1.24    bouyer 	} else if (iswap16(dirp->d_reclen) < entrysize) {
    467   1.1       cgd 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    468   1.1       cgd 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
    469   1.1       cgd 		inp->i_dotdot = (ino_t)-1;
    470  1.24    bouyer 		markclean = 0;
    471   1.1       cgd 	} else if (inp->i_parent != 0) {
    472   1.1       cgd 		/*
    473   1.1       cgd 		 * We know the parent, so fix now.
    474   1.1       cgd 		 */
    475   1.1       cgd 		inp->i_dotdot = inp->i_parent;
    476   1.1       cgd 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    477   1.1       cgd 		proto.d_reclen = dirp->d_reclen;
    478  1.20     lukem 		memmove(dirp, &proto, (size_t)entrysize);
    479   1.1       cgd 		if (reply("FIX") == 1)
    480   1.1       cgd 			ret |= ALTERED;
    481  1.24    bouyer 		else
    482  1.24    bouyer 			markclean = 0;
    483   1.1       cgd 	}
    484   1.1       cgd 	idesc->id_entryno++;
    485   1.1       cgd 	if (dirp->d_ino != 0)
    486  1.33      fvdl 		inoinfo(iswap32(dirp->d_ino))->ino_linkcnt--;
    487   1.1       cgd 	return (ret|KEEPON);
    488   1.1       cgd chk2:
    489   1.1       cgd 	if (dirp->d_ino == 0)
    490   1.1       cgd 		return (ret|KEEPON);
    491   1.1       cgd 	if (dirp->d_namlen <= 2 &&
    492   1.1       cgd 	    dirp->d_name[0] == '.' &&
    493   1.1       cgd 	    idesc->id_entryno >= 2) {
    494   1.1       cgd 		if (dirp->d_namlen == 1) {
    495   1.1       cgd 			direrror(idesc->id_number, "EXTRA '.' ENTRY");
    496   1.1       cgd 			dirp->d_ino = 0;
    497   1.1       cgd 			if (reply("FIX") == 1)
    498   1.1       cgd 				ret |= ALTERED;
    499  1.24    bouyer 			else
    500  1.24    bouyer 				markclean = 0;
    501   1.1       cgd 			return (KEEPON | ret);
    502   1.1       cgd 		}
    503   1.1       cgd 		if (dirp->d_name[1] == '.') {
    504   1.1       cgd 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
    505   1.1       cgd 			dirp->d_ino = 0;
    506   1.1       cgd 			if (reply("FIX") == 1)
    507   1.1       cgd 				ret |= ALTERED;
    508  1.24    bouyer 			else
    509  1.24    bouyer 				markclean = 0;
    510   1.1       cgd 			return (KEEPON | ret);
    511   1.1       cgd 		}
    512   1.1       cgd 	}
    513   1.1       cgd 	idesc->id_entryno++;
    514   1.1       cgd 	n = 0;
    515  1.24    bouyer 	if (iswap32(dirp->d_ino) > maxino) {
    516   1.1       cgd 		fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
    517   1.1       cgd 		n = reply("REMOVE");
    518  1.24    bouyer 		if (n == 0)
    519  1.24    bouyer 			markclean = 0;
    520  1.10   mycroft 	} else if (newinofmt &&
    521  1.24    bouyer 		   ((iswap32(dirp->d_ino) == WINO && dirp->d_type != DT_WHT) ||
    522  1.24    bouyer 		    (iswap32(dirp->d_ino) != WINO && dirp->d_type == DT_WHT))) {
    523  1.24    bouyer 		fileerror(idesc->id_number, iswap32(dirp->d_ino), "BAD WHITEOUT ENTRY");
    524  1.24    bouyer 		dirp->d_ino = iswap32(WINO);
    525  1.10   mycroft 		dirp->d_type = DT_WHT;
    526  1.10   mycroft 		if (reply("FIX") == 1)
    527  1.10   mycroft 			ret |= ALTERED;
    528  1.24    bouyer 		else
    529  1.24    bouyer 			markclean = 0;
    530   1.1       cgd 	} else {
    531   1.1       cgd again:
    532  1.33      fvdl 		info = inoinfo(iswap32(dirp->d_ino));
    533  1.33      fvdl 		switch (info->ino_state) {
    534   1.1       cgd 		case USTATE:
    535   1.1       cgd 			if (idesc->id_entryno <= 2)
    536   1.1       cgd 				break;
    537  1.24    bouyer 			fileerror(idesc->id_number, iswap32(dirp->d_ino), "UNALLOCATED");
    538   1.1       cgd 			n = reply("REMOVE");
    539  1.24    bouyer 			if (n == 0)
    540  1.24    bouyer 				markclean = 0;
    541   1.1       cgd 			break;
    542   1.1       cgd 
    543   1.1       cgd 		case DCLEAR:
    544   1.1       cgd 		case FCLEAR:
    545   1.1       cgd 			if (idesc->id_entryno <= 2)
    546   1.1       cgd 				break;
    547  1.33      fvdl 			if (info->ino_state == FCLEAR)
    548   1.6   mycroft 				errmsg = "DUP/BAD";
    549  1.26      fvdl 			else if (!preen && !usedsoftdep)
    550   1.1       cgd 				errmsg = "ZERO LENGTH DIRECTORY";
    551   1.6   mycroft 			else {
    552   1.6   mycroft 				n = 1;
    553   1.6   mycroft 				break;
    554   1.6   mycroft 			}
    555  1.24    bouyer 			fileerror(idesc->id_number, iswap32(dirp->d_ino), errmsg);
    556   1.1       cgd 			if ((n = reply("REMOVE")) == 1)
    557   1.1       cgd 				break;
    558  1.24    bouyer 			dp = ginode(iswap32(dirp->d_ino));
    559  1.33      fvdl 			info->ino_state =
    560  1.33      fvdl 			    (iswap16(DIP(dp, mode)) & IFMT) == IFDIR ? DSTATE : FSTATE;
    561  1.33      fvdl 			info->ino_linkcnt = iswap16(DIP(dp, nlink));
    562   1.1       cgd 			goto again;
    563   1.1       cgd 
    564   1.1       cgd 		case DSTATE:
    565   1.1       cgd 		case DFOUND:
    566  1.24    bouyer 			inp = getinoinfo(iswap32(dirp->d_ino));
    567   1.1       cgd 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
    568  1.35    itojun 				getpathname(pathbuf, sizeof(pathbuf),
    569  1.35    itojun 				    idesc->id_number, idesc->id_number);
    570  1.35    itojun 				getpathname(namebuf, sizeof(namebuf),
    571  1.35    itojun 				    iswap32(dirp->d_ino), iswap32(dirp->d_ino));
    572   1.1       cgd 				pwarn("%s %s %s\n", pathbuf,
    573   1.1       cgd 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
    574   1.1       cgd 				    namebuf);
    575   1.1       cgd 				if (preen)
    576   1.1       cgd 					printf(" (IGNORED)\n");
    577   1.1       cgd 				else if ((n = reply("REMOVE")) == 1)
    578   1.1       cgd 					break;
    579   1.1       cgd 			}
    580   1.1       cgd 			if (idesc->id_entryno > 2)
    581   1.1       cgd 				inp->i_parent = idesc->id_number;
    582   1.1       cgd 			/* fall through */
    583   1.1       cgd 
    584   1.1       cgd 		case FSTATE:
    585  1.33      fvdl 			if (newinofmt && dirp->d_type != info->ino_type) {
    586  1.24    bouyer 				fileerror(idesc->id_number, iswap32(dirp->d_ino),
    587   1.6   mycroft 				    "BAD TYPE VALUE");
    588  1.33      fvdl 				dirp->d_type = info->ino_type;
    589   1.6   mycroft 				if (reply("FIX") == 1)
    590   1.6   mycroft 					ret |= ALTERED;
    591  1.24    bouyer 				else
    592  1.24    bouyer 					markclean = 0;
    593   1.6   mycroft 			}
    594  1.33      fvdl 			info->ino_linkcnt--;
    595   1.1       cgd 			break;
    596   1.1       cgd 
    597   1.1       cgd 		default:
    598  1.45  christos 			errexit("BAD STATE %d FOR INODE I=%d",
    599  1.33      fvdl 			    info->ino_state, iswap32(dirp->d_ino));
    600   1.1       cgd 		}
    601   1.1       cgd 	}
    602   1.1       cgd 	if (n == 0)
    603   1.1       cgd 		return (ret|KEEPON);
    604   1.1       cgd 	dirp->d_ino = 0;
    605   1.1       cgd 	return (ret|KEEPON|ALTERED);
    606   1.1       cgd }
    607   1.1       cgd 
    608   1.1       cgd /*
    609   1.1       cgd  * Routine to sort disk blocks.
    610   1.1       cgd  */
    611  1.16  christos static int
    612  1.40   xtraeme blksort(const void *arg1, const void *arg2)
    613   1.1       cgd {
    614  1.20     lukem 
    615  1.42  christos 	return ((*(const struct inoinfo *const *)arg1)->i_blks[0] -
    616  1.42  christos 		(*(const struct inoinfo *const *)arg2)->i_blks[0]);
    617   1.1       cgd }
    618