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