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