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