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