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