Home | History | Annotate | Line # | Download | only in fsck_lfs
utilities.c revision 1.10
      1 /* $NetBSD: utilities.c,v 1.10 2003/01/24 21:55:10 fvdl 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 %lld TO DISK\n",
    218 		 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
    219 		       (long long)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 %lld", mesg, (long long)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(" %lld (%lld),",
    346 				    (long long)((blk*dev_bsize + i) / secsize),
    347 				    (long long)(blk + i / dev_bsize));
    348 			else
    349 				printf(" %lld,",
    350 				    (long long)(blk + i / dev_bsize));
    351 			errs++;
    352 		}
    353 	}
    354 	printf("\n");
    355 	return (errs);
    356 }
    357 
    358 void
    359 bwrite(int fd, char *buf, daddr_t blk, long size)
    360 {
    361 	int             i;
    362 	char           *cp;
    363 	off_t           offset;
    364 
    365 	if (fd < 0)
    366 		return;
    367 	offset = blk;
    368 	offset *= dev_bsize;
    369 	if (lseek(fd, offset, 0) < 0)
    370 		rwerror("SEEK", blk);
    371 	else if (write(fd, buf, (int)size) == size) {
    372 		fsmodified = 1;
    373 		return;
    374 	}
    375 	rwerror("WRITE", blk);
    376 	if (lseek(fd, offset, 0) < 0)
    377 		rwerror("SEEK", blk);
    378 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
    379 	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
    380 		if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
    381 			(void)lseek(fd, offset + i + dev_bsize, 0);
    382 			printf(" %lld,", (long long)(blk + i / dev_bsize));
    383 		}
    384 	printf("\n");
    385 	return;
    386 }
    387 
    388 /*
    389  * allocate a data block with the specified number of fragments
    390  */
    391 int
    392 allocblk(long frags)
    393 {
    394 #if 1
    395 	/*
    396 	 * XXX Can't allocate blocks right now because we would have to do
    397 	 * a full partial segment write.
    398 	 */
    399 	return 0;
    400 #else				/* 0 */
    401 	register int    i, j, k;
    402 
    403 	if (frags <= 0 || frags > sblock.lfs_frag)
    404 		return (0);
    405 	for (i = 0; i < maxfsblock - sblock.lfs_frag; i += sblock.lfs_frag) {
    406 		for (j = 0; j <= sblock.lfs_frag - frags; j++) {
    407 			if (testbmap(i + j))
    408 				continue;
    409 			for (k = 1; k < frags; k++)
    410 				if (testbmap(i + j + k))
    411 					break;
    412 			if (k < frags) {
    413 				j += k;
    414 				continue;
    415 			}
    416 			for (k = 0; k < frags; k++) {
    417 #ifndef VERBOSE_BLOCKMAP
    418 				setbmap(i + j + k);
    419 #else
    420 				setbmap(i + j + k, -1);
    421 #endif
    422 			}
    423 			n_blks += frags;
    424 			return (i + j);
    425 		}
    426 	}
    427 	return (0);
    428 #endif				/* 0 */
    429 }
    430 
    431 /*
    432  * Free a previously allocated block
    433  */
    434 void
    435 freeblk(daddr_t blkno, long frags)
    436 {
    437 	struct inodesc  idesc;
    438 
    439 	idesc.id_blkno = blkno;
    440 	idesc.id_numfrags = frags;
    441 	(void)pass4check(&idesc);
    442 }
    443 
    444 /*
    445  * Find a pathname
    446  */
    447 void
    448 getpathname(char *namebuf, ino_t curdir, ino_t ino)
    449 {
    450 	int             len;
    451 	register char  *cp;
    452 	struct inodesc  idesc;
    453 	static int      busy = 0;
    454 
    455 	if (curdir == ino && ino == ROOTINO) {
    456 		(void)strcpy(namebuf, "/");
    457 		return;
    458 	}
    459 	if (busy ||
    460 	    (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
    461 		(void)strcpy(namebuf, "?");
    462 		return;
    463 	}
    464 	busy = 1;
    465 	memset(&idesc, 0, sizeof(struct inodesc));
    466 	idesc.id_type = DATA;
    467 	idesc.id_fix = IGNORE;
    468 	cp = &namebuf[MAXPATHLEN - 1];
    469 	*cp = '\0';
    470 	if (curdir != ino) {
    471 		idesc.id_parent = curdir;
    472 		goto namelookup;
    473 	}
    474 	while (ino != ROOTINO) {
    475 		idesc.id_number = ino;
    476 		idesc.id_func = findino;
    477 		idesc.id_name = "..";
    478 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
    479 			break;
    480 namelookup:
    481 		idesc.id_number = idesc.id_parent;
    482 		idesc.id_parent = ino;
    483 		idesc.id_func = findname;
    484 		idesc.id_name = namebuf;
    485 		if ((ckinode(ginode(idesc.id_number), &idesc) & FOUND) == 0)
    486 			break;
    487 		len = strlen(namebuf);
    488 		cp -= len;
    489 		memcpy(cp, namebuf, (size_t)len);
    490 		*--cp = '/';
    491 		if (cp < &namebuf[MAXNAMLEN])
    492 			break;
    493 		ino = idesc.id_number;
    494 	}
    495 	busy = 0;
    496 	if (ino != ROOTINO)
    497 		*--cp = '?';
    498 	memcpy(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
    499 }
    500 
    501 void
    502 catch(int n)
    503 {
    504 	if (!doinglevel2)
    505 		ckfini(0);
    506 	exit(12);
    507 }
    508 
    509 /*
    510  * When preening, allow a single quit to signal
    511  * a special exit after filesystem checks complete
    512  * so that reboot sequence may be interrupted.
    513  */
    514 void
    515 catchquit(int n)
    516 {
    517 	printf("returning to single-user after filesystem check\n");
    518 	returntosingle = 1;
    519 	(void)signal(SIGQUIT, SIG_DFL);
    520 }
    521 
    522 /*
    523  * Ignore a single quit signal; wait and flush just in case.
    524  * Used by child processes in preen.
    525  */
    526 void
    527 voidquit(int n)
    528 {
    529 
    530 	sleep(1);
    531 	(void)signal(SIGQUIT, SIG_IGN);
    532 	(void)signal(SIGQUIT, SIG_DFL);
    533 }
    534 
    535 /*
    536  * determine whether an inode should be fixed.
    537  */
    538 int
    539 dofix(struct inodesc * idesc, char *msg)
    540 {
    541 
    542 	switch (idesc->id_fix) {
    543 
    544 		case DONTKNOW:
    545 		if (idesc->id_type == DATA)
    546 			direrror(idesc->id_number, msg);
    547 		else
    548 			pwarn("%s", msg);
    549 		if (preen) {
    550 			printf(" (SALVAGED)\n");
    551 			idesc->id_fix = FIX;
    552 			return (ALTERED);
    553 		}
    554 		if (reply("SALVAGE") == 0) {
    555 			idesc->id_fix = NOFIX;
    556 			return (0);
    557 		}
    558 		idesc->id_fix = FIX;
    559 		return (ALTERED);
    560 
    561 	case FIX:
    562 		return (ALTERED);
    563 
    564 	case NOFIX:
    565 	case IGNORE:
    566 		return (0);
    567 
    568 	default:
    569 		errexit("UNKNOWN INODESC FIX MODE %d\n", idesc->id_fix);
    570 	}
    571 	/* NOTREACHED */
    572 }
    573