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