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