Home | History | Annotate | Line # | Download | only in fsck_ffs
pass2.c revision 1.15
      1 /*	$NetBSD: pass2.c,v 1.15 1996/06/11 07:07:56 mycroft 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 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)pass2.c	8.6 (Berkeley) 10/27/94";
     39 #else
     40 static char rcsid[] = "$NetBSD: pass2.c,v 1.15 1996/06/11 07:07:56 mycroft Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/param.h>
     45 #include <sys/time.h>
     46 #include <ufs/ufs/dinode.h>
     47 #include <ufs/ufs/dir.h>
     48 #include <ufs/ffs/fs.h>
     49 
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include "fsck.h"
     54 #include "extern.h"
     55 
     56 #define MINDIRSIZE	(sizeof (struct dirtemplate))
     57 
     58 int	pass2check(), blksort();
     59 
     60 void
     61 pass2()
     62 {
     63 	register struct dinode *dp;
     64 	register struct inoinfo **inpp, *inp;
     65 	struct inoinfo **inpend;
     66 	struct inodesc curino;
     67 	struct dinode dino;
     68 	char pathbuf[MAXPATHLEN + 1];
     69 
     70 	switch (statemap[ROOTINO]) {
     71 
     72 	case USTATE:
     73 		pfatal("ROOT INODE UNALLOCATED");
     74 		if (reply("ALLOCATE") == 0)
     75 			errexit("");
     76 		if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
     77 			errexit("CANNOT ALLOCATE ROOT INODE\n");
     78 		break;
     79 
     80 	case DCLEAR:
     81 		pfatal("DUPS/BAD IN ROOT INODE");
     82 		if (reply("REALLOCATE")) {
     83 			freeino(ROOTINO);
     84 			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
     85 				errexit("CANNOT ALLOCATE ROOT INODE\n");
     86 			break;
     87 		}
     88 		if (reply("CONTINUE") == 0)
     89 			errexit("");
     90 		break;
     91 
     92 	case FSTATE:
     93 	case FCLEAR:
     94 		pfatal("ROOT INODE NOT DIRECTORY");
     95 		if (reply("REALLOCATE")) {
     96 			freeino(ROOTINO);
     97 			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
     98 				errexit("CANNOT ALLOCATE ROOT INODE\n");
     99 			break;
    100 		}
    101 		if (reply("FIX") == 0)
    102 			errexit("");
    103 		dp = ginode(ROOTINO);
    104 		dp->di_mode &= ~IFMT;
    105 		dp->di_mode |= IFDIR;
    106 		inodirty();
    107 		break;
    108 
    109 	case DSTATE:
    110 		break;
    111 
    112 	default:
    113 		errexit("BAD STATE %d FOR ROOT INODE", statemap[ROOTINO]);
    114 	}
    115 	if (newinofmt) {
    116 		statemap[WINO] = FSTATE;
    117 		typemap[WINO] = DT_WHT;
    118 	}
    119 	/*
    120 	 * Sort the directory list into disk block order.
    121 	 */
    122 	qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
    123 	/*
    124 	 * Check the integrity of each directory.
    125 	 */
    126 	memset(&curino, 0, sizeof(struct inodesc));
    127 	curino.id_type = DATA;
    128 	curino.id_func = pass2check;
    129 	inpend = &inpsort[inplast];
    130 	for (inpp = inpsort; inpp < inpend; inpp++) {
    131 		inp = *inpp;
    132 		if (inp->i_isize == 0)
    133 			continue;
    134 		if (inp->i_isize < MINDIRSIZE) {
    135 			direrror(inp->i_number, "DIRECTORY TOO SHORT");
    136 			inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
    137 			if (reply("FIX") == 1) {
    138 				dp = ginode(inp->i_number);
    139 				dp->di_size = inp->i_isize;
    140 				inodirty();
    141 			}
    142 		} else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
    143 			getpathname(pathbuf, inp->i_number, inp->i_number);
    144 			pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d",
    145 				pathbuf, inp->i_isize, DIRBLKSIZ);
    146 			if (preen)
    147 				printf(" (ADJUSTED)\n");
    148 			inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
    149 			if (preen || reply("ADJUST") == 1) {
    150 				dp = ginode(inp->i_number);
    151 				dp->di_size = inp->i_isize;
    152 				inodirty();
    153 			}
    154 		}
    155 		memset(&dino, 0, sizeof(struct dinode));
    156 		dino.di_mode = IFDIR;
    157 		dino.di_size = inp->i_isize;
    158 		memcpy(&dino.di_db[0], &inp->i_blks[0], (size_t)inp->i_numblks);
    159 		curino.id_number = inp->i_number;
    160 		curino.id_parent = inp->i_parent;
    161 		(void)ckinode(&dino, &curino);
    162 	}
    163 	/*
    164 	 * Now that the parents of all directories have been found,
    165 	 * make another pass to verify the value of `..'
    166 	 */
    167 	for (inpp = inpsort; inpp < inpend; inpp++) {
    168 		inp = *inpp;
    169 		if (inp->i_parent == 0 || inp->i_isize == 0)
    170 			continue;
    171 		if (inp->i_dotdot == inp->i_parent ||
    172 		    inp->i_dotdot == (ino_t)-1)
    173 			continue;
    174 		if (inp->i_dotdot == 0) {
    175 			inp->i_dotdot = inp->i_parent;
    176 			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
    177 			if (reply("FIX") == 0)
    178 				continue;
    179 			(void)makeentry(inp->i_number, inp->i_parent, "..");
    180 			lncntp[inp->i_parent]--;
    181 			continue;
    182 		}
    183 		fileerror(inp->i_parent, inp->i_number,
    184 		    "BAD INODE NUMBER FOR '..'");
    185 		if (reply("FIX") == 0)
    186 			continue;
    187 		lncntp[inp->i_dotdot]++;
    188 		lncntp[inp->i_parent]--;
    189 		inp->i_dotdot = inp->i_parent;
    190 		(void)changeino(inp->i_number, "..", inp->i_parent);
    191 	}
    192 	/*
    193 	 * Mark all the directories that can be found from the root.
    194 	 */
    195 	propagate();
    196 }
    197 
    198 int
    199 pass2check(idesc)
    200 	struct inodesc *idesc;
    201 {
    202 	register struct direct *dirp = idesc->id_dirp;
    203 	register struct inoinfo *inp;
    204 	int n, entrysize, ret = 0;
    205 	struct dinode *dp;
    206 	char *errmsg;
    207 	struct direct proto;
    208 	char namebuf[MAXPATHLEN + 1];
    209 	char pathbuf[MAXPATHLEN + 1];
    210 
    211 	/*
    212 	 * If converting, set directory entry type.
    213 	 */
    214 	if (doinglevel2 && dirp->d_ino > 0 && dirp->d_ino < maxino) {
    215 		dirp->d_type = typemap[dirp->d_ino];
    216 		ret |= ALTERED;
    217 	}
    218 	/*
    219 	 * check for "."
    220 	 */
    221 	if (idesc->id_entryno != 0)
    222 		goto chk1;
    223 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
    224 		if (dirp->d_ino != idesc->id_number) {
    225 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
    226 			dirp->d_ino = idesc->id_number;
    227 			if (reply("FIX") == 1)
    228 				ret |= ALTERED;
    229 		}
    230 		if (newinofmt && dirp->d_type != DT_DIR) {
    231 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
    232 			dirp->d_type = DT_DIR;
    233 			if (reply("FIX") == 1)
    234 				ret |= ALTERED;
    235 		}
    236 		goto chk1;
    237 	}
    238 	direrror(idesc->id_number, "MISSING '.'");
    239 	proto.d_ino = idesc->id_number;
    240 	if (newinofmt)
    241 		proto.d_type = DT_DIR;
    242 	else
    243 		proto.d_type = 0;
    244 	proto.d_namlen = 1;
    245 	(void)strcpy(proto.d_name, ".");
    246 #	if BYTE_ORDER == LITTLE_ENDIAN
    247 		if (!newinofmt) {
    248 			u_char tmp;
    249 
    250 			tmp = proto.d_type;
    251 			proto.d_type = proto.d_namlen;
    252 			proto.d_namlen = tmp;
    253 		}
    254 #	endif
    255 	entrysize = DIRSIZ(0, &proto);
    256 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
    257 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
    258 			dirp->d_name);
    259 	} else if (dirp->d_reclen < entrysize) {
    260 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
    261 	} else if (dirp->d_reclen < 2 * entrysize) {
    262 		proto.d_reclen = dirp->d_reclen;
    263 		memcpy(dirp, &proto, (size_t)entrysize);
    264 		if (reply("FIX") == 1)
    265 			ret |= ALTERED;
    266 	} else {
    267 		n = dirp->d_reclen - entrysize;
    268 		proto.d_reclen = entrysize;
    269 		memcpy(dirp, &proto, (size_t)entrysize);
    270 		idesc->id_entryno++;
    271 		lncntp[dirp->d_ino]--;
    272 		dirp = (struct direct *)((char *)(dirp) + entrysize);
    273 		memset(dirp, 0, (size_t)n);
    274 		dirp->d_reclen = n;
    275 		if (reply("FIX") == 1)
    276 			ret |= ALTERED;
    277 	}
    278 chk1:
    279 	if (idesc->id_entryno > 1)
    280 		goto chk2;
    281 	inp = getinoinfo(idesc->id_number);
    282 	proto.d_ino = inp->i_parent;
    283 	if (newinofmt)
    284 		proto.d_type = DT_DIR;
    285 	else
    286 		proto.d_type = 0;
    287 	proto.d_namlen = 2;
    288 	(void)strcpy(proto.d_name, "..");
    289 #	if BYTE_ORDER == LITTLE_ENDIAN
    290 		if (!newinofmt) {
    291 			u_char tmp;
    292 
    293 			tmp = proto.d_type;
    294 			proto.d_type = proto.d_namlen;
    295 			proto.d_namlen = tmp;
    296 		}
    297 #	endif
    298 	entrysize = DIRSIZ(0, &proto);
    299 	if (idesc->id_entryno == 0) {
    300 		n = DIRSIZ(0, dirp);
    301 		if (dirp->d_reclen < n + entrysize)
    302 			goto chk2;
    303 		proto.d_reclen = dirp->d_reclen - n;
    304 		dirp->d_reclen = n;
    305 		idesc->id_entryno++;
    306 		lncntp[dirp->d_ino]--;
    307 		dirp = (struct direct *)((char *)(dirp) + n);
    308 		memset(dirp, 0, (size_t)proto.d_reclen);
    309 		dirp->d_reclen = proto.d_reclen;
    310 	}
    311 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
    312 		inp->i_dotdot = dirp->d_ino;
    313 		if (newinofmt && dirp->d_type != DT_DIR) {
    314 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
    315 			dirp->d_type = DT_DIR;
    316 			if (reply("FIX") == 1)
    317 				ret |= ALTERED;
    318 		}
    319 		goto chk2;
    320 	}
    321 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
    322 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    323 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
    324 			dirp->d_name);
    325 		inp->i_dotdot = (ino_t)-1;
    326 	} else if (dirp->d_reclen < entrysize) {
    327 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    328 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
    329 		inp->i_dotdot = (ino_t)-1;
    330 	} else if (inp->i_parent != 0) {
    331 		/*
    332 		 * We know the parent, so fix now.
    333 		 */
    334 		inp->i_dotdot = inp->i_parent;
    335 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    336 		proto.d_reclen = dirp->d_reclen;
    337 		memcpy(dirp, &proto, (size_t)entrysize);
    338 		if (reply("FIX") == 1)
    339 			ret |= ALTERED;
    340 	}
    341 	idesc->id_entryno++;
    342 	if (dirp->d_ino != 0)
    343 		lncntp[dirp->d_ino]--;
    344 	return (ret|KEEPON);
    345 chk2:
    346 	if (dirp->d_ino == 0)
    347 		return (ret|KEEPON);
    348 	if (dirp->d_namlen <= 2 &&
    349 	    dirp->d_name[0] == '.' &&
    350 	    idesc->id_entryno >= 2) {
    351 		if (dirp->d_namlen == 1) {
    352 			direrror(idesc->id_number, "EXTRA '.' ENTRY");
    353 			dirp->d_ino = 0;
    354 			if (reply("FIX") == 1)
    355 				ret |= ALTERED;
    356 			return (KEEPON | ret);
    357 		}
    358 		if (dirp->d_name[1] == '.') {
    359 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
    360 			dirp->d_ino = 0;
    361 			if (reply("FIX") == 1)
    362 				ret |= ALTERED;
    363 			return (KEEPON | ret);
    364 		}
    365 	}
    366 	idesc->id_entryno++;
    367 	n = 0;
    368 	if (dirp->d_ino > maxino) {
    369 		fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
    370 		n = reply("REMOVE");
    371 	} else if (newinofmt &&
    372 		   ((dirp->d_ino == WINO && dirp->d_type != DT_WHT) ||
    373 		    (dirp->d_ino != WINO && dirp->d_type == DT_WHT))) {
    374 		fileerror(idesc->id_number, dirp->d_ino, "BAD WHITEOUT ENTRY");
    375 		dirp->d_ino = WINO;
    376 		dirp->d_type = DT_WHT;
    377 		if (reply("FIX") == 1)
    378 			ret |= ALTERED;
    379 	} else {
    380 again:
    381 		switch (statemap[dirp->d_ino]) {
    382 		case USTATE:
    383 			if (idesc->id_entryno <= 2)
    384 				break;
    385 			fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
    386 			n = reply("REMOVE");
    387 			break;
    388 
    389 		case DCLEAR:
    390 		case FCLEAR:
    391 			if (idesc->id_entryno <= 2)
    392 				break;
    393 			if (statemap[dirp->d_ino] == FCLEAR)
    394 				errmsg = "DUP/BAD";
    395 			else if (!preen)
    396 				errmsg = "ZERO LENGTH DIRECTORY";
    397 			else {
    398 				n = 1;
    399 				break;
    400 			}
    401 			fileerror(idesc->id_number, dirp->d_ino, errmsg);
    402 			if ((n = reply("REMOVE")) == 1)
    403 				break;
    404 			dp = ginode(dirp->d_ino);
    405 			statemap[dirp->d_ino] =
    406 			    (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE;
    407 			lncntp[dirp->d_ino] = dp->di_nlink;
    408 			goto again;
    409 
    410 		case DSTATE:
    411 		case DFOUND:
    412 			inp = getinoinfo(dirp->d_ino);
    413 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
    414 				getpathname(pathbuf, idesc->id_number,
    415 				    idesc->id_number);
    416 				getpathname(namebuf, dirp->d_ino, dirp->d_ino);
    417 				pwarn("%s %s %s\n", pathbuf,
    418 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
    419 				    namebuf);
    420 				if (preen)
    421 					printf(" (IGNORED)\n");
    422 				else if ((n = reply("REMOVE")) == 1)
    423 					break;
    424 			}
    425 			if (idesc->id_entryno > 2)
    426 				inp->i_parent = idesc->id_number;
    427 			/* fall through */
    428 
    429 		case FSTATE:
    430 			if (newinofmt && dirp->d_type != typemap[dirp->d_ino]) {
    431 				fileerror(idesc->id_number, dirp->d_ino,
    432 				    "BAD TYPE VALUE");
    433 				dirp->d_type = typemap[dirp->d_ino];
    434 				if (reply("FIX") == 1)
    435 					ret |= ALTERED;
    436 			}
    437 			lncntp[dirp->d_ino]--;
    438 			break;
    439 
    440 		default:
    441 			errexit("BAD STATE %d FOR INODE I=%d",
    442 			    statemap[dirp->d_ino], dirp->d_ino);
    443 		}
    444 	}
    445 	if (n == 0)
    446 		return (ret|KEEPON);
    447 	dirp->d_ino = 0;
    448 	return (ret|KEEPON|ALTERED);
    449 }
    450 
    451 /*
    452  * Routine to sort disk blocks.
    453  */
    454 int
    455 blksort(inpp1, inpp2)
    456 	struct inoinfo **inpp1, **inpp2;
    457 {
    458 
    459 	return ((*inpp1)->i_blks[0] - (*inpp2)->i_blks[0]);
    460 }
    461