Home | History | Annotate | Line # | Download | only in fsck_lfs
pass2.c revision 1.33
      1 /* $NetBSD: pass2.c,v 1.33 2015/09/21 01:22:18 dholland 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/types.h>
     33 #include <sys/param.h>
     34 #include <sys/time.h>
     35 #include <sys/mount.h>
     36 #include <sys/buf.h>
     37 
     38 #include <ufs/lfs/lfs.h>
     39 #include <ufs/lfs/lfs_accessors.h>
     40 #include <ufs/lfs/lfs_inode.h>
     41 
     42 #include <err.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 
     47 #include "bufcache.h"
     48 #include "vnode.h"
     49 #include "lfs_user.h"
     50 
     51 #include "fsck.h"
     52 #include "fsutil.h"
     53 #include "extern.h"
     54 
     55 #define MINDIRSIZE	(sizeof (struct lfs_dirtemplate))
     56 
     57 static int pass2check(struct inodesc *);
     58 static int blksort(const void *, const void *);
     59 
     60 void
     61 pass2(void)
     62 {
     63 	union lfs_dinode *dp;
     64 	struct uvnode *vp;
     65 	struct inoinfo **inpp, *inp;
     66 	struct inoinfo **inpend;
     67 	struct inodesc curino;
     68 	union lfs_dinode dino;
     69 	char pathbuf[MAXPATHLEN + 1];
     70 	uint16_t mode;
     71 	unsigned ii;
     72 
     73 	switch (statemap[ULFS_ROOTINO]) {
     74 
     75 	case USTATE:
     76 		pfatal("ROOT INODE UNALLOCATED");
     77 		if (reply("ALLOCATE") == 0)
     78 			err(EEXIT, "%s", "");
     79 		if (allocdir(ULFS_ROOTINO, ULFS_ROOTINO, 0755) != ULFS_ROOTINO)
     80 			err(EEXIT, "CANNOT ALLOCATE ROOT INODE");
     81 		break;
     82 
     83 	case DCLEAR:
     84 		pfatal("DUPS/BAD IN ROOT INODE");
     85 		if (reply("REALLOCATE")) {
     86 			freeino(ULFS_ROOTINO);
     87 			if (allocdir(ULFS_ROOTINO, ULFS_ROOTINO, 0755) != ULFS_ROOTINO)
     88 				err(EEXIT, "CANNOT ALLOCATE ROOT INODE");
     89 			break;
     90 		}
     91 		if (reply("CONTINUE") == 0)
     92 			err(EEXIT, "%s", "");
     93 		break;
     94 
     95 	case FSTATE:
     96 	case FCLEAR:
     97 		pfatal("ROOT INODE NOT DIRECTORY");
     98 		if (reply("REALLOCATE")) {
     99 			freeino(ULFS_ROOTINO);
    100 			if (allocdir(ULFS_ROOTINO, ULFS_ROOTINO, 0755) != ULFS_ROOTINO)
    101 				err(EEXIT, "CANNOT ALLOCATE ROOT INODE");
    102 			break;
    103 		}
    104 		if (reply("FIX") == 0)
    105 			errx(EEXIT, "%s", "");
    106 		vp = vget(fs, ULFS_ROOTINO);
    107 		dp = VTOD(vp);
    108 		mode = lfs_dino_getmode(fs, dp);
    109 		mode &= ~LFS_IFMT;
    110 		mode |= LFS_IFDIR;
    111 		lfs_dino_setmode(fs, dp, mode);
    112 		inodirty(VTOI(vp));
    113 		break;
    114 
    115 	case DSTATE:
    116 		break;
    117 
    118 	default:
    119 		errx(EEXIT, "BAD STATE %d FOR ROOT INODE", statemap[ULFS_ROOTINO]);
    120 	}
    121 	statemap[ULFS_WINO] = FSTATE;
    122 	typemap[ULFS_WINO] = LFS_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, LFS_DIRBLKSIZ);
    141 			if (reply("FIX") == 1) {
    142 				vp = vget(fs, inp->i_number);
    143 				dp = VTOD(vp);
    144 				lfs_dino_setsize(fs, dp, inp->i_isize);
    145 				inodirty(VTOI(vp));
    146 			}
    147 		} else if ((inp->i_isize & (LFS_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, LFS_DIRBLKSIZ);
    152 			if (preen)
    153 				printf(" (ADJUSTED)\n");
    154 			inp->i_isize = roundup(inp->i_isize, LFS_DIRBLKSIZ);
    155 			if (preen || reply("ADJUST") == 1) {
    156 				vp = vget(fs, inp->i_number);
    157 				dp = VTOD(vp);
    158 				lfs_dino_setsize(fs, dp, inp->i_isize);
    159 				inodirty(VTOI(vp));
    160 			}
    161 		}
    162 		memset(&dino, 0, sizeof(dino));
    163 		lfs_dino_setmode(fs, &dino, LFS_IFDIR);
    164 		lfs_dino_setsize(fs, &dino, inp->i_isize);
    165 		for (ii = 0; ii < inp->i_numblks / sizeof(inp->i_blks[0]) &&
    166 			     ii < ULFS_NDADDR; ii++) {
    167 			lfs_dino_setdb(fs, &dino, ii, inp->i_blks[ii]);
    168 		}
    169 		for (; ii < inp->i_numblks / sizeof(inp->i_blks[0]); ii++) {
    170 			lfs_dino_setib(fs, &dino, ii - ULFS_NDADDR,
    171 				       inp->i_blks[ii]);
    172 		}
    173 		curino.id_number = inp->i_number;
    174 		curino.id_parent = inp->i_parent;
    175 		(void) ckinode(&dino, &curino);
    176 	}
    177 	/*
    178 	 * Now that the parents of all directories have been found,
    179 	 * make another pass to verify the value of `..'
    180 	 */
    181 	for (inpp = inpsort; inpp < inpend; inpp++) {
    182 		inp = *inpp;
    183 		if (inp->i_parent == 0 || inp->i_isize == 0)
    184 			continue;
    185 		if (inp->i_dotdot == inp->i_parent ||
    186 		    inp->i_dotdot == (ino_t) - 1)
    187 			continue;
    188 		if (inp->i_dotdot == 0) {
    189 			inp->i_dotdot = inp->i_parent;
    190 			fileerror(inp->i_parent, inp->i_number, "MISSING '..'");
    191 			if (reply("FIX") == 0)
    192 				continue;
    193 			(void) makeentry(inp->i_number, inp->i_parent, "..");
    194 			lncntp[inp->i_parent]--;
    195 			continue;
    196 		}
    197 		fileerror(inp->i_parent, inp->i_number,
    198 		    "BAD INODE NUMBER FOR '..'");
    199 		if (reply("FIX") == 0)
    200 			continue;
    201 		lncntp[inp->i_dotdot]++;
    202 		lncntp[inp->i_parent]--;
    203 		inp->i_dotdot = inp->i_parent;
    204 		(void) changeino(inp->i_number, "..", inp->i_parent);
    205 	}
    206 	/*
    207 	 * Mark all the directories that can be found from the root.
    208 	 */
    209 	propagate();
    210 }
    211 
    212 static int
    213 pass2check(struct inodesc * idesc)
    214 {
    215 	struct lfs_dirheader *dirp = idesc->id_dirp;
    216 	struct inoinfo *inp;
    217 	int n, entrysize, ret = 0;
    218 	union lfs_dinode *dp;
    219 	const char *errmsg;
    220 	struct lfs_dirheader proto;
    221 	char namebuf[MAXPATHLEN + 1];
    222 	char pathbuf[MAXPATHLEN + 1];
    223 
    224 	/*
    225 	 * check for "."
    226 	 */
    227 	if (idesc->id_entryno != 0)
    228 		goto chk1;
    229 	if (lfs_dir_getino(fs, dirp) != 0 && strcmp(lfs_dir_nameptr(fs, dirp), ".") == 0) {
    230 		if (lfs_dir_getino(fs, dirp) != idesc->id_number) {
    231 			direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'");
    232 			if (reply("FIX") == 1) {
    233 				lfs_dir_setino(fs, dirp, idesc->id_number);
    234 				ret |= ALTERED;
    235 			}
    236 		}
    237 		if (lfs_dir_gettype(fs, dirp) != LFS_DT_DIR) {
    238 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'");
    239 			if (reply("FIX") == 1) {
    240 				lfs_dir_settype(fs, dirp, LFS_DT_DIR);
    241 				ret |= ALTERED;
    242 			}
    243 		}
    244 		goto chk1;
    245 	}
    246 	direrror(idesc->id_number, "MISSING '.'");
    247 	lfs_dir_setino(fs, &proto, idesc->id_number);
    248 	lfs_dir_settype(fs, &proto, LFS_DT_DIR);
    249 	lfs_dir_setnamlen(fs, &proto, 1);
    250 	entrysize = LFS_DIRECTSIZ(fs, 1);
    251 	lfs_dir_setreclen(fs, &proto, entrysize);
    252 	if (lfs_dir_getino(fs, dirp) != 0 && strcmp(lfs_dir_nameptr(fs, dirp), "..") != 0) {
    253 		pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n",
    254 		    lfs_dir_nameptr(fs, dirp));
    255 	} else if (lfs_dir_getreclen(fs, dirp) < entrysize) {
    256 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n");
    257 	} else if (lfs_dir_getreclen(fs, dirp) < 2 * entrysize) {
    258 		/* convert this entry to a . entry */
    259 		lfs_dir_setreclen(fs, &proto, lfs_dir_getreclen(fs, dirp));
    260 		memcpy(dirp, &proto, sizeof(proto));
    261 		lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), ".", 1,
    262 				lfs_dir_getreclen(fs, dirp));
    263 		if (reply("FIX") == 1)
    264 			ret |= ALTERED;
    265 	} else {
    266 		/* split this entry and use the beginning for the . entry */
    267 		n = lfs_dir_getreclen(fs, dirp) - entrysize;
    268 		memcpy(dirp, &proto, sizeof(proto));
    269 		lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), ".", 1,
    270 				lfs_dir_getreclen(fs, dirp));
    271 		idesc->id_entryno++;
    272 		lncntp[lfs_dir_getino(fs, dirp)]--;
    273 		dirp = LFS_NEXTDIR(fs, dirp);
    274 		memset(dirp, 0, (size_t) n);
    275 		lfs_dir_setreclen(fs, dirp, n);
    276 		if (reply("FIX") == 1)
    277 			ret |= ALTERED;
    278 	}
    279 chk1:
    280 	if (idesc->id_entryno > 1)
    281 		goto chk2;
    282 	inp = getinoinfo(idesc->id_number);
    283 	lfs_dir_setino(fs, &proto, inp->i_parent);
    284 	lfs_dir_settype(fs, &proto, LFS_DT_DIR);
    285 	lfs_dir_setnamlen(fs, &proto, 2);
    286 	entrysize = LFS_DIRECTSIZ(fs, 2);
    287 	lfs_dir_setreclen(fs, &proto, entrysize);
    288 	if (idesc->id_entryno == 0) {
    289 		n = LFS_DIRSIZ(fs, dirp);
    290 		if (lfs_dir_getreclen(fs, dirp) < n + entrysize)
    291 			goto chk2;
    292 		lfs_dir_setreclen(fs, &proto, lfs_dir_getreclen(fs, dirp) - n);
    293 		lfs_dir_setreclen(fs, dirp, n);
    294 		idesc->id_entryno++;
    295 		lncntp[lfs_dir_getino(fs, dirp)]--;
    296 		dirp = (struct lfs_dirheader *) ((char *) (dirp) + n);
    297 		memset(dirp, 0, lfs_dir_getreclen(fs, &proto));
    298 		lfs_dir_setreclen(fs, dirp, lfs_dir_getreclen(fs, &proto));
    299 	}
    300 	if (lfs_dir_getino(fs, dirp) != 0 && strcmp(lfs_dir_nameptr(fs, dirp), "..") == 0) {
    301 		inp->i_dotdot = lfs_dir_getino(fs, dirp);
    302 		if (lfs_dir_gettype(fs, dirp) != LFS_DT_DIR) {
    303 			direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'");
    304 			lfs_dir_settype(fs, dirp, LFS_DT_DIR);
    305 			if (reply("FIX") == 1)
    306 				ret |= ALTERED;
    307 		}
    308 		goto chk2;
    309 	}
    310 	if (lfs_dir_getino(fs, dirp) != 0 && strcmp(lfs_dir_nameptr(fs, dirp), ".") != 0) {
    311 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    312 		pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n",
    313 		    lfs_dir_nameptr(fs, dirp));
    314 		inp->i_dotdot = (ino_t) - 1;
    315 	} else if (lfs_dir_getreclen(fs, dirp) < entrysize) {
    316 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    317 		pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n");
    318 		inp->i_dotdot = (ino_t) - 1;
    319 	} else if (inp->i_parent != 0) {
    320 		/*
    321 		 * We know the parent, so fix now.
    322 		 */
    323 		inp->i_dotdot = inp->i_parent;
    324 		fileerror(inp->i_parent, idesc->id_number, "MISSING '..'");
    325 		lfs_dir_setreclen(fs, &proto, lfs_dir_getreclen(fs, dirp));
    326 		memcpy(dirp, &proto, (size_t) entrysize);
    327 		lfs_copydirname(fs, lfs_dir_nameptr(fs, dirp), "..", 2,
    328 				lfs_dir_getreclen(fs, dirp));
    329 		if (reply("FIX") == 1)
    330 			ret |= ALTERED;
    331 	}
    332 	idesc->id_entryno++;
    333 	if (lfs_dir_getino(fs, dirp) != 0)
    334 		lncntp[lfs_dir_getino(fs, dirp)]--;
    335 	return (ret | KEEPON);
    336 chk2:
    337 	if (lfs_dir_getino(fs, dirp) == 0)
    338 		return (ret | KEEPON);
    339 	if (lfs_dir_getnamlen(fs, dirp) <= 2 &&
    340 	    lfs_dir_nameptr(fs, dirp)[0] == '.' &&
    341 	    idesc->id_entryno >= 2) {
    342 		if (lfs_dir_getnamlen(fs, dirp) == 1) {
    343 			direrror(idesc->id_number, "EXTRA '.' ENTRY");
    344 			if (reply("FIX") == 1) {
    345 				lfs_dir_setino(fs, dirp, 0);
    346 				ret |= ALTERED;
    347 			}
    348 			return (KEEPON | ret);
    349 		}
    350 		if (lfs_dir_nameptr(fs, dirp)[1] == '.') {
    351 			direrror(idesc->id_number, "EXTRA '..' ENTRY");
    352 			if (reply("FIX") == 1) {
    353 				lfs_dir_setino(fs, dirp, 0);
    354 				ret |= ALTERED;
    355 			}
    356 			return (KEEPON | ret);
    357 		}
    358 	}
    359 	idesc->id_entryno++;
    360 	n = 0;
    361 	if (lfs_dir_getino(fs, dirp) >= maxino) {
    362 		fileerror(idesc->id_number, lfs_dir_getino(fs, dirp), "I OUT OF RANGE");
    363 		n = reply("REMOVE");
    364 	} else if (lfs_dir_getino(fs, dirp) == LFS_IFILE_INUM &&
    365 	    idesc->id_number == ULFS_ROOTINO) {
    366 		if (lfs_dir_gettype(fs, dirp) != LFS_DT_REG) {
    367 			fileerror(idesc->id_number, lfs_dir_getino(fs, dirp),
    368 			    "BAD TYPE FOR IFILE");
    369 			if (reply("FIX") == 1) {
    370 				lfs_dir_settype(fs, dirp, LFS_DT_REG);
    371 				ret |= ALTERED;
    372 			}
    373 		}
    374 	} else if (((lfs_dir_getino(fs, dirp) == ULFS_WINO && lfs_dir_gettype(fs, dirp) != LFS_DT_WHT) ||
    375 		(lfs_dir_getino(fs, dirp) != ULFS_WINO && lfs_dir_gettype(fs, dirp) == LFS_DT_WHT))) {
    376 		fileerror(idesc->id_number, lfs_dir_getino(fs, dirp), "BAD WHITEOUT ENTRY");
    377 		if (reply("FIX") == 1) {
    378 			lfs_dir_setino(fs, dirp, ULFS_WINO);
    379 			lfs_dir_settype(fs, dirp, LFS_DT_WHT);
    380 			ret |= ALTERED;
    381 		}
    382 	} else {
    383 again:
    384 		switch (statemap[lfs_dir_getino(fs, dirp)]) {
    385 		case USTATE:
    386 			if (idesc->id_entryno <= 2)
    387 				break;
    388 			fileerror(idesc->id_number, lfs_dir_getino(fs, dirp),
    389 			    "UNALLOCATED");
    390 			n = reply("REMOVE");
    391 			break;
    392 
    393 		case DCLEAR:
    394 		case FCLEAR:
    395 			if (idesc->id_entryno <= 2)
    396 				break;
    397 			if (statemap[lfs_dir_getino(fs, dirp)] == FCLEAR)
    398 				errmsg = "DUP/BAD";
    399 			else if (!preen)
    400 				errmsg = "ZERO LENGTH DIRECTORY";
    401 			else {
    402 				n = 1;
    403 				break;
    404 			}
    405 			fileerror(idesc->id_number, lfs_dir_getino(fs, dirp), errmsg);
    406 			if ((n = reply("REMOVE")) == 1)
    407 				break;
    408 			dp = ginode(lfs_dir_getino(fs, dirp));
    409 			statemap[lfs_dir_getino(fs, dirp)] =
    410 			    (lfs_dino_getmode(fs, dp) & LFS_IFMT) == LFS_IFDIR ? DSTATE : FSTATE;
    411 			lncntp[lfs_dir_getino(fs, dirp)] = lfs_dino_getnlink(fs, dp);
    412 			goto again;
    413 
    414 		case DSTATE:
    415 		case DFOUND:
    416 			inp = getinoinfo(lfs_dir_getino(fs, dirp));
    417 			if (inp->i_parent != 0 && idesc->id_entryno > 2) {
    418 				getpathname(pathbuf, sizeof(pathbuf),
    419 				    idesc->id_number, idesc->id_number);
    420 				getpathname(namebuf, sizeof(namebuf),
    421 				    lfs_dir_getino(fs, dirp),
    422 				    lfs_dir_getino(fs, dirp));
    423 				pwarn("%s %s %s\n", pathbuf,
    424 				    "IS AN EXTRANEOUS HARD LINK TO DIRECTORY",
    425 				    namebuf);
    426 				if (preen)
    427 					printf(" (IGNORED)\n");
    428 				else if ((n = reply("REMOVE")) == 1)
    429 					break;
    430 			}
    431 			if (idesc->id_entryno > 2)
    432 				inp->i_parent = idesc->id_number;
    433 			/* fall through */
    434 
    435 		case FSTATE:
    436 			if (lfs_dir_gettype(fs, dirp) != typemap[lfs_dir_getino(fs, dirp)]) {
    437 				fileerror(idesc->id_number,
    438 				    lfs_dir_getino(fs, dirp),
    439 				    "BAD TYPE VALUE");
    440 				if (debug)
    441 					pwarn("dir has %d, typemap has %d\n",
    442 						lfs_dir_gettype(fs, dirp), typemap[lfs_dir_getino(fs, dirp)]);
    443 				lfs_dir_settype(fs, dirp, typemap[lfs_dir_getino(fs, dirp)]);
    444 				if (reply("FIX") == 1)
    445 					ret |= ALTERED;
    446 			}
    447 			lncntp[lfs_dir_getino(fs, dirp)]--;
    448 			break;
    449 
    450 		default:
    451 			errx(EEXIT, "BAD STATE %d FOR INODE I=%ju",
    452 			    statemap[lfs_dir_getino(fs, dirp)],
    453 			    (uintmax_t)lfs_dir_getino(fs, dirp));
    454 		}
    455 	}
    456 	if (n == 0)
    457 		return (ret | KEEPON);
    458 	lfs_dir_setino(fs, dirp, 0);
    459 	return (ret | KEEPON | ALTERED);
    460 }
    461 /*
    462  * Routine to sort disk blocks.
    463  */
    464 static int
    465 blksort(const void *inpp1, const void *inpp2)
    466 {
    467 	return ((*(const struct inoinfo *const *) inpp1)->i_blks[0] -
    468 	    (*(const struct inoinfo *const *) inpp2)->i_blks[0]);
    469 }
    470