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