Home | History | Annotate | Line # | Download | only in fsck_ext2fs
utilities.c revision 1.13
      1 /*	$NetBSD: utilities.c,v 1.13 2005/02/11 06:21:21 simonb 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 /*
     33  * Copyright (c) 1997 Manuel Bouyer.
     34  *
     35  * Redistribution and use in source and binary forms, with or without
     36  * modification, are permitted provided that the following conditions
     37  * are met:
     38  * 1. Redistributions of source code must retain the above copyright
     39  *    notice, this list of conditions and the following disclaimer.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice, this list of conditions and the following disclaimer in the
     42  *    documentation and/or other materials provided with the distribution.
     43  * 3. All advertising materials mentioning features or use of this software
     44  *    must display the following acknowledgement:
     45  *	This product includes software developed by Manuel Bouyer.
     46  * 4. The name of the author may not be used to endorse or promote products
     47  *    derived from this software without specific prior written permission.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     50  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     51  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     52  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     53  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     54  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     55  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     56  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     58  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     59  */
     60 
     61 #include <sys/cdefs.h>
     62 #ifndef lint
     63 #if 0
     64 static char sccsid[] = "@(#)utilities.c	8.1 (Berkeley) 6/5/93";
     65 #else
     66 __RCSID("$NetBSD: utilities.c,v 1.13 2005/02/11 06:21:21 simonb Exp $");
     67 #endif
     68 #endif /* not lint */
     69 
     70 #include <sys/param.h>
     71 #include <sys/time.h>
     72 #include <ufs/ext2fs/ext2fs_dinode.h>
     73 #include <ufs/ext2fs/ext2fs_dir.h>
     74 #include <ufs/ext2fs/ext2fs.h>
     75 #include <ufs/ufs/dinode.h> /* for IFMT & friends */
     76 #include <stdio.h>
     77 #include <stdlib.h>
     78 #include <string.h>
     79 #include <ctype.h>
     80 #include <unistd.h>
     81 
     82 #include "fsutil.h"
     83 #include "fsck.h"
     84 #include "extern.h"
     85 
     86 long	diskreads, totalreads;	/* Disk cache statistics */
     87 
     88 static void rwerror(char *, daddr_t);
     89 
     90 extern int returntosingle;
     91 
     92 int
     93 ftypeok(struct ext2fs_dinode *dp)
     94 {
     95 	switch (fs2h16(dp->e2di_mode) & IFMT) {
     96 
     97 	case IFDIR:
     98 	case IFREG:
     99 	case IFBLK:
    100 	case IFCHR:
    101 	case IFLNK:
    102 	case IFSOCK:
    103 	case IFIFO:
    104 		return (1);
    105 
    106 	default:
    107 		if (debug)
    108 			printf("bad file type 0%o\n", fs2h16(dp->e2di_mode));
    109 		return (0);
    110 	}
    111 }
    112 
    113 int
    114 reply(char *question)
    115 {
    116 	int persevere;
    117 	char c;
    118 
    119 	if (preen)
    120 		pfatal("INTERNAL ERROR: GOT TO reply()");
    121 	persevere = !strcmp(question, "CONTINUE");
    122 	printf("\n");
    123 	if (!persevere && (nflag || fswritefd < 0)) {
    124 		printf("%s? no\n\n", question);
    125 		return (0);
    126 	}
    127 	if (yflag || (persevere && nflag)) {
    128 		printf("%s? yes\n\n", question);
    129 		return (1);
    130 	}
    131 	do	{
    132 		printf("%s? [yn] ", question);
    133 		(void) fflush(stdout);
    134 		c = getc(stdin);
    135 		while (c != '\n' && getc(stdin) != '\n')
    136 			if (feof(stdin))
    137 				return (0);
    138 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
    139 	printf("\n");
    140 	if (c == 'y' || c == 'Y')
    141 		return (1);
    142 	return (0);
    143 }
    144 
    145 /*
    146  * Malloc buffers and set up cache.
    147  */
    148 void
    149 bufinit(void)
    150 {
    151 	struct bufarea *bp;
    152 	long bufcnt, i;
    153 	char *bufp;
    154 
    155 	diskreads = totalreads = 0;
    156 	pbp = pdirbp = (struct bufarea *)0;
    157 	bufhead.b_next = bufhead.b_prev = &bufhead;
    158 	bufcnt = MAXBUFSPACE / sblock.e2fs_bsize;
    159 	if (bufcnt < MINBUFS)
    160 		bufcnt = MINBUFS;
    161 	for (i = 0; i < bufcnt; i++) {
    162 		bp = (struct bufarea *)malloc(sizeof(struct bufarea));
    163 		bufp = malloc((unsigned int)sblock.e2fs_bsize);
    164 		if (bp == NULL || bufp == NULL) {
    165 			if (i >= MINBUFS)
    166 				break;
    167 			errexit("cannot allocate buffer pool\n");
    168 		}
    169 		bp->b_un.b_buf = bufp;
    170 		bp->b_prev = &bufhead;
    171 		bp->b_next = bufhead.b_next;
    172 		bufhead.b_next->b_prev = bp;
    173 		bufhead.b_next = bp;
    174 		initbarea(bp);
    175 	}
    176 	bufhead.b_size = i;	/* save number of buffers */
    177 }
    178 
    179 /*
    180  * Manage a cache of directory blocks.
    181  */
    182 struct bufarea *
    183 getdatablk(daddr_t blkno, long size)
    184 {
    185 	struct bufarea *bp;
    186 
    187 	for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
    188 		if (bp->b_bno == fsbtodb(&sblock, blkno))
    189 			goto foundit;
    190 	for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
    191 		if ((bp->b_flags & B_INUSE) == 0)
    192 			break;
    193 	if (bp == &bufhead)
    194 		errexit("deadlocked buffer pool\n");
    195 	getblk(bp, blkno, size);
    196 	diskreads++;
    197 	/* fall through */
    198 foundit:
    199 	totalreads++;
    200 	bp->b_prev->b_next = bp->b_next;
    201 	bp->b_next->b_prev = bp->b_prev;
    202 	bp->b_prev = &bufhead;
    203 	bp->b_next = bufhead.b_next;
    204 	bufhead.b_next->b_prev = bp;
    205 	bufhead.b_next = bp;
    206 	bp->b_flags |= B_INUSE;
    207 	return (bp);
    208 }
    209 
    210 void
    211 getblk(struct bufarea *bp, daddr_t blk, long size)
    212 {
    213 	daddr_t dblk;
    214 
    215 	dblk = fsbtodb(&sblock, blk);
    216 	if (bp->b_bno != dblk) {
    217 		flush(fswritefd, bp);
    218 		bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size);
    219 		bp->b_bno = dblk;
    220 		bp->b_size = size;
    221 	}
    222 }
    223 
    224 void
    225 flush(int fd, struct bufarea *bp)
    226 {
    227 	int i;
    228 
    229 	if (!bp->b_dirty)
    230 		return;
    231 	if (bp->b_errs != 0)
    232 		pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
    233 		    (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
    234 		    (long long)bp->b_bno);
    235 	bp->b_dirty = 0;
    236 	bp->b_errs = 0;
    237 	bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
    238 	if (bp != &sblk)
    239 		return;
    240 	for (i = 0; i < sblock.e2fs_ngdb; i++) {
    241 		bwrite(fswritefd, (char *)
    242 			&sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)],
    243 		    fsbtodb(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1),
    244 		    sblock.e2fs_bsize);
    245 	}
    246 }
    247 
    248 static void
    249 rwerror(char *mesg, daddr_t blk)
    250 {
    251 
    252 	if (preen == 0)
    253 		printf("\n");
    254 	pfatal("CANNOT %s: BLK %lld", mesg, (long long)blk);
    255 	if (reply("CONTINUE") == 0)
    256 		errexit("Program terminated\n");
    257 }
    258 
    259 void
    260 ckfini(int markclean)
    261 {
    262 	struct bufarea *bp, *nbp;
    263 	int cnt = 0;
    264 
    265 	if (fswritefd < 0) {
    266 		(void)close(fsreadfd);
    267 		return;
    268 	}
    269 	flush(fswritefd, &sblk);
    270 	if (havesb && sblk.b_bno != SBOFF / dev_bsize &&
    271 	    !preen && reply("UPDATE STANDARD SUPERBLOCKS")) {
    272 		sblk.b_bno = SBOFF / dev_bsize;
    273 		sbdirty();
    274 		flush(fswritefd, &sblk);
    275 		copyback_sb(&asblk);
    276 		asblk.b_dirty = 1;
    277 		flush(fswritefd, &asblk);
    278 	}
    279 	for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
    280 		cnt++;
    281 		flush(fswritefd, bp);
    282 		nbp = bp->b_prev;
    283 		free(bp->b_un.b_buf);
    284 		free((char *)bp);
    285 	}
    286 	if (bufhead.b_size != cnt)
    287 		errexit("Panic: lost %d buffers\n", bufhead.b_size - cnt);
    288 	pbp = pdirbp = (struct bufarea *)0;
    289 	if (markclean && (sblock.e2fs.e2fs_state & E2FS_ISCLEAN) == 0) {
    290 		/*
    291 		 * Mark the file system as clean, and sync the superblock.
    292 		 */
    293 		if (preen)
    294 			pwarn("MARKING FILE SYSTEM CLEAN\n");
    295 		else if (!reply("MARK FILE SYSTEM CLEAN"))
    296 			markclean = 0;
    297 		if (markclean) {
    298 			sblock.e2fs.e2fs_state = E2FS_ISCLEAN;
    299 			sbdirty();
    300 			flush(fswritefd, &sblk);
    301 		}
    302 	}
    303 	if (debug)
    304 		printf("cache missed %ld of %ld (%d%%)\n", diskreads,
    305 		    totalreads, (int)(diskreads * 100 / totalreads));
    306 	(void)close(fsreadfd);
    307 	(void)close(fswritefd);
    308 }
    309 
    310 int
    311 bread(int fd, char *buf, daddr_t blk, long size)
    312 {
    313 	char *cp;
    314 	int i, errs;
    315 	off_t offset;
    316 
    317 	offset = blk;
    318 	offset *= dev_bsize;
    319 	if (lseek(fd, offset, 0) < 0)
    320 		rwerror("SEEK", blk);
    321 	else if (read(fd, buf, (int)size) == size)
    322 		return (0);
    323 	rwerror("READ", blk);
    324 	if (lseek(fd, offset, 0) < 0)
    325 		rwerror("SEEK", blk);
    326 	errs = 0;
    327 	memset(buf, 0, (size_t)size);
    328 	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
    329 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
    330 		if (read(fd, cp, (int)secsize) != secsize) {
    331 			(void)lseek(fd, offset + i + secsize, 0);
    332 			if (secsize != dev_bsize && dev_bsize != 1)
    333 				printf(" %lld (%lld),",
    334 				    (long long)((blk*dev_bsize + i) / secsize),
    335 				    (long long)(blk + i / dev_bsize));
    336 			else
    337 				printf(" %lld,", (long long)(blk +
    338 							i / dev_bsize));
    339 			errs++;
    340 		}
    341 	}
    342 	printf("\n");
    343 	return (errs);
    344 }
    345 
    346 void
    347 bwrite(int fd, char *buf, daddr_t blk, long size)
    348 {
    349 	int i;
    350 	char *cp;
    351 	off_t offset;
    352 
    353 	if (fd < 0)
    354 		return;
    355 	offset = blk;
    356 	offset *= dev_bsize;
    357 	if (lseek(fd, offset, 0) < 0)
    358 		rwerror("SEEK", blk);
    359 	else if (write(fd, buf, (int)size) == size) {
    360 		fsmodified = 1;
    361 		return;
    362 	}
    363 	rwerror("WRITE", blk);
    364 	if (lseek(fd, offset, 0) < 0)
    365 		rwerror("SEEK", blk);
    366 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
    367 	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
    368 		if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
    369 			(void)lseek(fd, offset + i + dev_bsize, 0);
    370 			printf(" %lld,", (long long)(blk + i / dev_bsize));
    371 		}
    372 	printf("\n");
    373 	return;
    374 }
    375 
    376 /*
    377  * allocate a data block
    378  */
    379 int
    380 allocblk(void)
    381 {
    382 	int i;
    383 
    384 	for (i = 0; i < maxfsblock - 1; i++) {
    385 		if (testbmap(i))
    386 				continue;
    387 		setbmap(i);
    388 		n_blks++;
    389 		return (i);
    390 	}
    391 	return (0);
    392 }
    393 
    394 /*
    395  * Free a previously allocated block
    396  */
    397 void
    398 freeblk(daddr_t blkno)
    399 {
    400 	struct inodesc idesc;
    401 
    402 	idesc.id_blkno = blkno;
    403 	idesc.id_numfrags = 1;
    404 	(void)pass4check(&idesc);
    405 }
    406 
    407 /*
    408  * Find a pathname
    409  */
    410 void
    411 getpathname(char *namebuf, size_t namebuflen, ino_t curdir, ino_t ino)
    412 {
    413 	int len;
    414 	char *cp;
    415 	struct inodesc idesc;
    416 	static int busy = 0;
    417 
    418 	if (curdir == ino && ino == EXT2_ROOTINO) {
    419 		(void)strlcpy(namebuf, "/", namebuflen);
    420 		return;
    421 	}
    422 	if (busy ||
    423 	    (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
    424 		(void)strlcpy(namebuf, "?", namebuflen);
    425 		return;
    426 	}
    427 	busy = 1;
    428 	memset(&idesc, 0, sizeof(struct inodesc));
    429 	idesc.id_type = DATA;
    430 	idesc.id_fix = IGNORE;
    431 	cp = &namebuf[MAXPATHLEN - 1];
    432 	*cp = '\0';
    433 	if (curdir != ino) {
    434 		idesc.id_parent = curdir;
    435 		goto namelookup;
    436 	}
    437 	while (ino != EXT2_ROOTINO) {
    438 		idesc.id_number = ino;
    439 		idesc.id_func = findino;
    440 		idesc.id_name = "..";
    441 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
    442 			break;
    443 	namelookup:
    444 		idesc.id_number = idesc.id_parent;
    445 		idesc.id_parent = ino;
    446 		idesc.id_func = findname;
    447 		idesc.id_name = namebuf;
    448 		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
    449 			break;
    450 		len = strlen(namebuf);
    451 		cp -= len;
    452 		memcpy(cp, namebuf, (size_t)len);
    453 		*--cp = '/';
    454 		if (cp < &namebuf[EXT2FS_MAXNAMLEN])
    455 			break;
    456 		ino = idesc.id_number;
    457 	}
    458 	busy = 0;
    459 	if (ino != EXT2_ROOTINO)
    460 		*--cp = '?';
    461 	memcpy(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
    462 }
    463 
    464 void
    465 catch(int n)
    466 {
    467 	ckfini(0);
    468 	exit(12);
    469 }
    470 
    471 /*
    472  * When preening, allow a single quit to signal
    473  * a special exit after filesystem checks complete
    474  * so that reboot sequence may be interrupted.
    475  */
    476 void
    477 catchquit(int n)
    478 {
    479 	printf("returning to single-user after filesystem check\n");
    480 	returntosingle = 1;
    481 	(void)signal(SIGQUIT, SIG_DFL);
    482 }
    483 
    484 /*
    485  * Ignore a single quit signal; wait and flush just in case.
    486  * Used by child processes in preen.
    487  */
    488 void
    489 voidquit(int n)
    490 {
    491 
    492 	sleep(1);
    493 	(void)signal(SIGQUIT, SIG_IGN);
    494 	(void)signal(SIGQUIT, SIG_DFL);
    495 }
    496 
    497 /*
    498  * determine whether an inode should be fixed.
    499  */
    500 int
    501 dofix(struct inodesc *idesc, char *msg)
    502 {
    503 
    504 	switch (idesc->id_fix) {
    505 
    506 	case DONTKNOW:
    507 		if (idesc->id_type == DATA)
    508 			direrror(idesc->id_number, msg);
    509 		else
    510 			pwarn("%s", msg);
    511 		if (preen) {
    512 			printf(" (SALVAGED)\n");
    513 			idesc->id_fix = FIX;
    514 			return (ALTERED);
    515 		}
    516 		if (reply("SALVAGE") == 0) {
    517 			idesc->id_fix = NOFIX;
    518 			return (0);
    519 		}
    520 		idesc->id_fix = FIX;
    521 		return (ALTERED);
    522 
    523 	case FIX:
    524 		return (ALTERED);
    525 
    526 	case NOFIX:
    527 	case IGNORE:
    528 		return (0);
    529 
    530 	default:
    531 		errexit("UNKNOWN INODESC FIX MODE %d\n", idesc->id_fix);
    532 	}
    533 	/* NOTREACHED */
    534 }
    535