Home | History | Annotate | Line # | Download | only in fsck_ffs
pass2.c revision 1.21
      1 /*	$NetBSD: pass2.c,v 1.21 1997/09/20 06:16:31 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.21 1997/09/20 06:16:31 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 	statemap[ROOTINO] = DFOUND;
    122 	if (newinofmt) {
    123 		statemap[WINO] = FSTATE;
    124 		typemap[WINO] = DT_WHT;
    125 	}
    126 	/*
    127 	 * Sort the directory list into disk block order.
    128 	 */
    129 	qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
    130 	/*
    131 	 * Check the integrity of each directory.
    132 	 */
    133 	memset(&curino, 0, sizeof(struct inodesc));
    134 	curino.id_type = DATA;
    135 	curino.id_func = pass2check;
    136 	dp = &dino;
    137 	inpend = &inpsort[inplast];
    138 	for (inpp = inpsort; inpp < inpend; inpp++) {
    139 		inp = *inpp;
    140 		if (inp->i_isize == 0)
    141 			continue;
    142 		if (inp->i_isize < MINDIRSIZE) {
    143 			direrror(inp->i_number, "DIRECTORY TOO SHORT");
    144 			inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
    145 			if (reply("FIX") == 1) {
    146 				dp = ginode(inp->i_number);
    147 				dp->di_size = inp->i_isize;
    148 				inodirty();
    149 				dp = &dino;
    150 			}
    151 		} else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
    152 			getpathname(pathbuf, inp->i_number, inp->i_number);
    153 			pwarn("DIRECTORY %s: LENGTH %lu NOT MULTIPLE OF %d",
    154 				pathbuf, (u_long)inp->i_isize, DIRBLKSIZ);
    155 			if (preen)
    156 				printf(" (ADJUSTED)\n");
    157 			inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
    158 			if (preen || reply("ADJUST") == 1) {
    159 				dp = ginode(inp->i_number);
    160 				dp->di_size = roundup(inp->i_isize, DIRBLKSIZ);
    161 				inodirty();
    162 				dp = &dino;
    163 			}
    164 		}
    165 		memset(&dino, 0, sizeof(struct dinode));
    166 		dino.di_mode = IFDIR;
    167 		dp->di_size = inp->i_isize;
    168 		memmove(&dp->di_db[0], &inp->i_blks[0], (size_t)inp->i_numblks);
    169 		curino.id_number = inp->i_number;
    170 		curino.id_parent = inp->i_parent;
    171 		(void)ckinode(dp, &curino);
    172 	}
    173 	/*
    174 	 * Now that the parents of all directories have been found,
    175 	 * make another pass to verify the value of `..'
    176 	 */
    177 	for (inpp = inpsort; inpp < inpend; inpp++) {
    178 		inp = *inpp;
    179 		if (inp->i_parent == 0 || inp->i_isize == 0)
    180 			continue;
    181 		if (statemap[inp->i_parent] == DFOUND &&
    182 		    statemap[inp->i_number] == DSTATE)
    183 			statemap[inp->i_number] = DFOUND;
    184 		if (inp->i_dotdot == inp->i_parent ||
    185 		    inp->i_dotdot == (ino_t)-1)
    186 			continue;
    187 		if (inp->i_dotdot == 0) {
    188 			inp->i_dotdot = inp->i_parent;
    189 			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
    190 			if (reply("FIX") == 0)
    191 				continue;
    192 			(void)makeentry(inp->i_number, inp->i_parent, "..");
    193 			lncntp[inp->i_parent]--;
    194 			continue;
    195 		}
    196 		fileerror(inp->i_parent, inp->i_number,
    197 		    "BAD INODE NUMBER FOR '..'");
    198 		if (reply("FIX") == 0)
    199 			continue;
    200 		lncntp[inp->i_dotdot]++;
    201 		lncntp[inp->i_parent]--;
    202 		inp->i_dotdot = inp->i_parent;
    203 		(void)changeino(inp->i_number, "..", inp->i_parent);
    204 	}
    205 	/*
    206 	 * Mark all the directories that can be found from the root.
    207 	 */
    208 	propagate();
    209 }
    210 
    211 static int
    212 pass2check(idesc)
    213 	struct inodesc *idesc;
    214 {
    215 	struct direct *dirp = idesc->id_dirp;
    216 	struct inoinfo *inp;
    217 	int n, entrysize, ret = 0;
    218 	struct dinode *dp;
    219 	char *errmsg;
    220 	struct direct proto;
    221 	char namebuf[MAXPATHLEN + 1];
    222 	char pathbuf[MAXPATHLEN + 1];
    223 
    224 	/*
    225 	 * If converting, set directory entry type.
    226 	 */
    227 	if (doinglevel2 && dirp->d_ino > 0 && dirp->d_ino < maxino) {
    228 		dirp->d_type = typemap[dirp->d_ino];
    229 		ret |= ALTERED;
    230 	}
    231 	/*
    232 	 * check for "."
    233 	 */
    234 	if (idesc->id_entryno != 0)
    235 		goto chk1;
    236 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
    237 		if (dirp->d_ino != idesc->id_number) {
    238 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
    239 			dirp->d_ino = idesc->id_number;
    240 			if (reply("FIX") == 1)
    241 				ret |= ALTERED;
    242 		}
    243 		if (newinofmt && dirp->d_type != DT_DIR) {
    244 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
    245 			dirp->d_type = DT_DIR;
    246 			if (reply("FIX") == 1)
    247 				ret |= ALTERED;
    248 		}
    249 		goto chk1;
    250 	}
    251 	direrror(idesc->id_number, "MISSING '.'");
    252 	proto.d_ino = idesc->id_number;
    253 	if (newinofmt)
    254 		proto.d_type = DT_DIR;
    255 	else
    256 		proto.d_type = 0;
    257 	proto.d_namlen = 1;
    258 	(void)strcpy(proto.d_name, ".");
    259 #	if BYTE_ORDER == LITTLE_ENDIAN
    260 		if (!newinofmt) {
    261 			u_char tmp;
    262 
    263 			tmp = proto.d_type;
    264 			proto.d_type = proto.d_namlen;
    265 			proto.d_namlen = tmp;
    266 		}
    267 #	endif
    268 	entrysize = DIRSIZ(0, &proto);
    269 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
    270 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
    271 			dirp->d_name);
    272 	} else if (dirp->d_reclen < entrysize) {
    273 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
    274 	} else if (dirp->d_reclen < 2 * entrysize) {
    275 		proto.d_reclen = dirp->d_reclen;
    276 		memmove(dirp, &proto, (size_t)entrysize);
    277 		if (reply("FIX") == 1)
    278 			ret |= ALTERED;
    279 	} else {
    280 		n = dirp->d_reclen - entrysize;
    281 		proto.d_reclen = entrysize;
    282 		memmove(dirp, &proto, (size_t)entrysize);
    283 		idesc->id_entryno++;
    284 		lncntp[dirp->d_ino]--;
    285 		dirp = (struct direct *)((char *)(dirp) + entrysize);
    286 		memset(dirp, 0, (size_t)n);
    287 		dirp->d_reclen = n;
    288 		if (reply("FIX") == 1)
    289 			ret |= ALTERED;
    290 	}
    291 chk1:
    292 	if (idesc->id_entryno > 1)
    293 		goto chk2;
    294 	inp = getinoinfo(idesc->id_number);
    295 	proto.d_ino = inp->i_parent;
    296 	if (newinofmt)
    297 		proto.d_type = DT_DIR;
    298 	else
    299 		proto.d_type = 0;
    300 	proto.d_namlen = 2;
    301 	(void)strcpy(proto.d_name, "..");
    302 #	if BYTE_ORDER == LITTLE_ENDIAN
    303 		if (!newinofmt) {
    304 			u_char tmp;
    305 
    306 			tmp = proto.d_type;
    307 			proto.d_type = proto.d_namlen;
    308 			proto.d_namlen = tmp;
    309 		}
    310 #	endif
    311 	entrysize = DIRSIZ(0, &proto);
    312 	if (idesc->id_entryno == 0) {
    313 		n = DIRSIZ(0, dirp);
    314 		if (dirp->d_reclen < n + entrysize)
    315 			goto chk2;
    316 		proto.d_reclen = dirp->d_reclen - n;
    317 		dirp->d_reclen = n;
    318 		idesc->id_entryno++;
    319 		lncntp[dirp->d_ino]--;
    320 		dirp = (struct direct *)((char *)(dirp) + n);
    321 		memset(dirp, 0, (size_t)proto.d_reclen);
    322 		dirp->d_reclen = proto.d_reclen;
    323 	}
    324 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
    325 		inp->i_dotdot = dirp->d_ino;
    326 		if (newinofmt && dirp->d_type != DT_DIR) {
    327 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
    328 			dirp->d_type = DT_DIR;
    329 			if (reply("FIX") == 1)
    330 				ret |= ALTERED;
    331 		}
    332 		goto chk2;
    333 	}
    334 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
    335 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    336 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
    337 			dirp->d_name);
    338 		inp->i_dotdot = (ino_t)-1;
    339 	} else if (dirp->d_reclen < entrysize) {
    340 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    341 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
    342 		inp->i_dotdot = (ino_t)-1;
    343 	} else if (inp->i_parent != 0) {
    344 		/*
    345 		 * We know the parent, so fix now.
    346 		 */
    347 		inp->i_dotdot = inp->i_parent;
    348 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    349 		proto.d_reclen = dirp->d_reclen;
    350 		memmove(dirp, &proto, (size_t)entrysize);
    351 		if (reply("FIX") == 1)
    352 			ret |= ALTERED;
    353 	}
    354 	idesc->id_entryno++;
    355 	if (dirp->d_ino != 0)
    356 		lncntp[dirp->d_ino]--;
    357 	return (ret|KEEPON);
    358 chk2:
    359 	if (dirp->d_ino == 0)
    360 		return (ret|KEEPON);
    361 	if (dirp->d_namlen <= 2 &&
    362 	    dirp->d_name[0] == '.' &&
    363 	    idesc->id_entryno >= 2) {
    364 		if (dirp->d_namlen == 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 		if (dirp->d_name[1] == '.') {
    372 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
    373 			dirp->d_ino = 0;
    374 			if (reply("FIX") == 1)
    375 				ret |= ALTERED;
    376 			return (KEEPON | ret);
    377 		}
    378 	}
    379 	idesc->id_entryno++;
    380 	n = 0;
    381 	if (dirp->d_ino > maxino) {
    382 		fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
    383 		n = reply("REMOVE");
    384 	} else if (newinofmt &&
    385 		   ((dirp->d_ino == WINO && dirp->d_type != DT_WHT) ||
    386 		    (dirp->d_ino != WINO && dirp->d_type == DT_WHT))) {
    387 		fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
    388 		dirp->d_ino = WINO;
    389 		dirp->d_type = DT_WHT;
    390 		if (reply("FIX") == 1)
    391 			ret |= ALTERED;
    392 	} else {
    393 again:
    394 		switch (statemap[dirp->d_ino]) {
    395 		case USTATE:
    396 			if (idesc->id_entryno <= 2)
    397 				break;
    398 			fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
    399 			n = reply("REMOVE");
    400 			break;
    401 
    402 		case DCLEAR:
    403 		case FCLEAR:
    404 			if (idesc->id_entryno <= 2)
    405 				break;
    406 			if (statemap[dirp->d_ino] == FCLEAR)
    407 				errmsg = "DUP/BAD";
    408 			else if (!preen)
    409 				errmsg = "ZERO LENGTH DIRECTORY";
    410 			else {
    411 				n = 1;
    412 				break;
    413 			}
    414 			fileerror(idesc->id_number, dirp->d_ino, errmsg);
    415 			if ((n = reply("REMOVE")) == 1)
    416 				break;
    417 			dp = ginode(dirp->d_ino);
    418 			statemap[dirp->d_ino] =
    419 			    (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE;
    420 			lncntp[dirp->d_ino] = dp->di_nlink;
    421 			goto again;
    422 
    423 		case DSTATE:
    424 			if (statemap[idesc->id_number] == DFOUND)
    425 				statemap[dirp->d_ino] = DFOUND;
    426 			/* fall through */
    427 
    428 		case DFOUND:
    429 			inp = getinoinfo(dirp->d_ino);
    430 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
    431 				getpathname(pathbuf, idesc->id_number,
    432 				    idesc->id_number);
    433 				getpathname(namebuf, dirp->d_ino, dirp->d_ino);
    434 				pwarn("%s %s %s\n", pathbuf,
    435 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
    436 				    namebuf);
    437 				if (preen)
    438 					printf(" (IGNORED)\n");
    439 				else if ((n = reply("REMOVE")) == 1)
    440 					break;
    441 			}
    442 			if (idesc->id_entryno > 2)
    443 				inp->i_parent = idesc->id_number;
    444 			/* fall through */
    445 
    446 		case FSTATE:
    447 			if (newinofmt && dirp->d_type != typemap[dirp->d_ino]) {
    448 				fileerror(idesc->id_number, dirp->d_ino,
    449 				    "BAD TYPE VALUE");
    450 				dirp->d_type = typemap[dirp->d_ino];
    451 				if (reply("FIX") == 1)
    452 					ret |= ALTERED;
    453 			}
    454 			lncntp[dirp->d_ino]--;
    455 			break;
    456 
    457 		default:
    458 			errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
    459 			    statemap[dirp->d_ino], dirp->d_ino);
    460 		}
    461 	}
    462 	if (n == 0)
    463 		return (ret|KEEPON);
    464 	dirp->d_ino = 0;
    465 	return (ret|KEEPON|ALTERED);
    466 }
    467 
    468 /*
    469  * Routine to sort disk blocks.
    470  */
    471 static int
    472 blksort(arg1, arg2)
    473 	const void *arg1, *arg2;
    474 {
    475 
    476 	return ((*(struct inoinfo **)arg1)->i_blks[0] -
    477 		(*(struct inoinfo **)arg2)->i_blks[0]);
    478 }
    479