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