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