Home | History | Annotate | Line # | Download | only in fsck_ffs
pass2.c revision 1.4
      1 /*
      2  * Copyright (c) 1980, 1986 The Regents of the University of California.
      3  * 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	5.17 (Berkeley) 12/28/90";*/
     36 static char rcsid[] = "$Id: pass2.c,v 1.4 1993/08/01 18:27:19 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 #include <sys/param.h>
     40 #include <ufs/dinode.h>
     41 #include <ufs/fs.h>
     42 #define KERNEL
     43 #include <ufs/dir.h>
     44 #undef KERNEL
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include "fsck.h"
     48 
     49 #define MINDIRSIZE	(sizeof (struct dirtemplate))
     50 
     51 int	pass2check(), blksort();
     52 
     53 pass2()
     54 {
     55 	register struct dinode *dp;
     56 	register struct inoinfo **inpp, *inp;
     57 	struct inoinfo **inpend;
     58 	struct inodesc curino;
     59 	struct dinode dino;
     60 	char pathbuf[MAXPATHLEN + 1];
     61 
     62 	switch (statemap[ROOTINO]) {
     63 
     64 	case USTATE:
     65 		pfatal("ROOT INODE UNALLOCATED");
     66 		if (reply("ALLOCATE") == 0)
     67 			errexit("");
     68 		if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
     69 			errexit("CANNOT ALLOCATE ROOT INODE\n");
     70 		break;
     71 
     72 	case DCLEAR:
     73 		pfatal("DUPS/BAD IN ROOT INODE");
     74 		if (reply("REALLOCATE")) {
     75 			freeino(ROOTINO);
     76 			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
     77 				errexit("CANNOT ALLOCATE ROOT INODE\n");
     78 			break;
     79 		}
     80 		if (reply("CONTINUE") == 0)
     81 			errexit("");
     82 		break;
     83 
     84 	case FSTATE:
     85 	case FCLEAR:
     86 		pfatal("ROOT INODE NOT DIRECTORY");
     87 		if (reply("REALLOCATE")) {
     88 			freeino(ROOTINO);
     89 			if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO)
     90 				errexit("CANNOT ALLOCATE ROOT INODE\n");
     91 			break;
     92 		}
     93 		if (reply("FIX") == 0)
     94 			errexit("");
     95 		dp = ginode(ROOTINO);
     96 		dp->di_mode &= ~IFMT;
     97 		dp->di_mode |= IFDIR;
     98 		inodirty();
     99 		break;
    100 
    101 	case DSTATE:
    102 		break;
    103 
    104 	default:
    105 		errexit("BAD STATE %d FOR ROOT INODE", statemap[ROOTINO]);
    106 	}
    107 	statemap[ROOTINO] = DFOUND;
    108 	/*
    109 	 * Sort the directory list into disk block order.
    110 	 */
    111 	qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort);
    112 	/*
    113 	 * Check the integrity of each directory.
    114 	 */
    115 	bzero((char *)&curino, sizeof(struct inodesc));
    116 	curino.id_type = DATA;
    117 	curino.id_func = pass2check;
    118 	dino.di_mode = IFDIR;
    119 	dp = &dino;
    120 	inpend = &inpsort[inplast];
    121 	for (inpp = inpsort; inpp < inpend; inpp++) {
    122 		inp = *inpp;
    123 		if (inp->i_isize == 0)
    124 			continue;
    125 		if (inp->i_isize < MINDIRSIZE) {
    126 			direrror(inp->i_number, "DIRECTORY TOO SHORT");
    127 			inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ);
    128 			if (reply("FIX") == 1) {
    129 				dp = ginode(inp->i_number);
    130 				dp->di_size = inp->i_isize;
    131 				inodirty();
    132 				dp = &dino;
    133 			}
    134 		} else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) {
    135 			getpathname(pathbuf, inp->i_number, inp->i_number);
    136 			pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d",
    137 				pathbuf, inp->i_isize, DIRBLKSIZ);
    138 			if (preen)
    139 				printf(" (ADJUSTED)\n");
    140 			inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ);
    141 			if (preen || reply("ADJUST") == 1) {
    142 				dp = ginode(inp->i_number);
    143 				dp->di_size = roundup(inp->i_isize, DIRBLKSIZ);
    144 				inodirty();
    145 				dp = &dino;
    146 			}
    147 		}
    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 	 * check for "."
    207 	 */
    208 	if (idesc->id_entryno != 0)
    209 		goto chk1;
    210 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) {
    211 		if (dirp->d_ino != idesc->id_number) {
    212 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
    213 			dirp->d_ino = idesc->id_number;
    214 			if (reply("FIX") == 1)
    215 				ret |= ALTERED;
    216 		}
    217 		goto chk1;
    218 	}
    219 	direrror(idesc->id_number, "MISSING '.'");
    220 	proto.d_ino = idesc->id_number;
    221 	proto.d_namlen = 1;
    222 	(void)strcpy(proto.d_name, ".");
    223 	entrysize = DIRSIZ(&proto);
    224 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) {
    225 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
    226 			dirp->d_name);
    227 	} else if (dirp->d_reclen < entrysize) {
    228 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
    229 	} else if (dirp->d_reclen < 2 * entrysize) {
    230 		proto.d_reclen = dirp->d_reclen;
    231 		bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
    232 		if (reply("FIX") == 1)
    233 			ret |= ALTERED;
    234 	} else {
    235 		n = dirp->d_reclen - entrysize;
    236 		proto.d_reclen = entrysize;
    237 		bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
    238 		idesc->id_entryno++;
    239 		lncntp[dirp->d_ino]--;
    240 		dirp = (struct direct *)((char *)(dirp) + entrysize);
    241 		bzero((char *)dirp, (size_t)n);
    242 		dirp->d_reclen = n;
    243 		if (reply("FIX") == 1)
    244 			ret |= ALTERED;
    245 	}
    246 chk1:
    247 	if (idesc->id_entryno > 1)
    248 		goto chk2;
    249 	inp = getinoinfo(idesc->id_number);
    250 	proto.d_ino = inp->i_parent;
    251 	proto.d_namlen = 2;
    252 	(void)strcpy(proto.d_name, "..");
    253 	entrysize = DIRSIZ(&proto);
    254 	if (idesc->id_entryno == 0) {
    255 		n = DIRSIZ(dirp);
    256 		if (dirp->d_reclen < n + entrysize)
    257 			goto chk2;
    258 		proto.d_reclen = dirp->d_reclen - n;
    259 		dirp->d_reclen = n;
    260 		idesc->id_entryno++;
    261 		lncntp[dirp->d_ino]--;
    262 		dirp = (struct direct *)((char *)(dirp) + n);
    263 		bzero((char *)dirp, (size_t)proto.d_reclen);
    264 		dirp->d_reclen = proto.d_reclen;
    265 	}
    266 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) {
    267 		inp->i_dotdot = dirp->d_ino;
    268 		goto chk2;
    269 	}
    270 	if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) {
    271 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    272 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
    273 			dirp->d_name);
    274 		inp->i_dotdot = (ino_t)-1;
    275 	} else if (dirp->d_reclen < entrysize) {
    276 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    277 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
    278 		inp->i_dotdot = (ino_t)-1;
    279 	} else if (inp->i_parent != 0) {
    280 		/*
    281 		 * We know the parent, so fix now.
    282 		 */
    283 		inp->i_dotdot = inp->i_parent;
    284 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    285 		proto.d_reclen = dirp->d_reclen;
    286 		bcopy((char *)&proto, (char *)dirp, (size_t)entrysize);
    287 		if (reply("FIX") == 1)
    288 			ret |= ALTERED;
    289 	}
    290 	idesc->id_entryno++;
    291 	if (dirp->d_ino != 0)
    292 		lncntp[dirp->d_ino]--;
    293 	return (ret|KEEPON);
    294 chk2:
    295 	if (dirp->d_ino == 0)
    296 		return (ret|KEEPON);
    297 	if (dirp->d_namlen <= 2 &&
    298 	    dirp->d_name[0] == '.' &&
    299 	    idesc->id_entryno >= 2) {
    300 		if (dirp->d_namlen == 1) {
    301 			direrror(idesc->id_number, "EXTRA '.' ENTRY");
    302 			dirp->d_ino = 0;
    303 			if (reply("FIX") == 1)
    304 				ret |= ALTERED;
    305 			return (KEEPON | ret);
    306 		}
    307 		if (dirp->d_name[1] == '.') {
    308 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
    309 			dirp->d_ino = 0;
    310 			if (reply("FIX") == 1)
    311 				ret |= ALTERED;
    312 			return (KEEPON | ret);
    313 		}
    314 	}
    315 	idesc->id_entryno++;
    316 	n = 0;
    317 	if (dirp->d_ino > maxino) {
    318 		fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE");
    319 		n = reply("REMOVE");
    320 	} else {
    321 again:
    322 		switch (statemap[dirp->d_ino]) {
    323 		case USTATE:
    324 			if (idesc->id_entryno <= 2)
    325 				break;
    326 			fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED");
    327 			n = reply("REMOVE");
    328 			break;
    329 
    330 		case DCLEAR:
    331 		case FCLEAR:
    332 			if (idesc->id_entryno <= 2)
    333 				break;
    334 			if (statemap[dirp->d_ino] == DCLEAR)
    335 				errmsg = "ZERO LENGTH DIRECTORY";
    336 			else
    337 				errmsg = "DUP/BAD";
    338 			fileerror(idesc->id_number, dirp->d_ino, errmsg);
    339 			if ((n = reply("REMOVE")) == 1)
    340 				break;
    341 			dp = ginode(dirp->d_ino);
    342 			statemap[dirp->d_ino] =
    343 			    (dp->di_mode & IFMT) == IFDIR ? DSTATE : FSTATE;
    344 			lncntp[dirp->d_ino] = dp->di_nlink;
    345 			goto again;
    346 
    347 		case DSTATE:
    348 			if (statemap[idesc->id_number] == DFOUND)
    349 				statemap[dirp->d_ino] = DFOUND;
    350 			/* fall through */
    351 
    352 		case DFOUND:
    353 			inp = getinoinfo(dirp->d_ino);
    354 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
    355 				getpathname(pathbuf, idesc->id_number,
    356 				    idesc->id_number);
    357 				getpathname(namebuf, dirp->d_ino, dirp->d_ino);
    358 				pwarn("%s %s %s\n", pathbuf,
    359 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
    360 				    namebuf);
    361 				if (preen)
    362 					printf(" (IGNORED)\n");
    363 				else if ((n = reply("REMOVE")) == 1)
    364 					break;
    365 			}
    366 			if (idesc->id_entryno > 2)
    367 				inp->i_parent = idesc->id_number;
    368 			/* fall through */
    369 
    370 		case FSTATE:
    371 			lncntp[dirp->d_ino]--;
    372 			break;
    373 
    374 		default:
    375 			errexit("BAD STATE %d FOR INODE I=%d",
    376 			    statemap[dirp->d_ino], dirp->d_ino);
    377 		}
    378 	}
    379 	if (n == 0)
    380 		return (ret|KEEPON);
    381 	dirp->d_ino = 0;
    382 	return (ret|KEEPON|ALTERED);
    383 }
    384 
    385 /*
    386  * Routine to sort disk blocks.
    387  */
    388 blksort(inpp1, inpp2)
    389 	struct inoinfo **inpp1, **inpp2;
    390 {
    391 
    392 	return ((*inpp1)->i_blks[0] - (*inpp2)->i_blks[0]);
    393 }
    394