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