Home | History | Annotate | Line # | Download | only in fsck_lfs
utilities.c revision 1.1
      1 /*	$Id: utilities.c,v 1.1 1999/03/18 02:02:19 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_SBPAD / dev_bsize &&
    270 	    !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
    271 		sblk.b_bno = LFS_SBPAD / dev_bsize;
    272 		sbdirty();
    273 		flush(fswritefd, &sblk);
    274 	}
    275 	/* flush(fswritefd, &cgblk); */
    276 	/* free(cgblk.b_un.b_buf); */
    277 	for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
    278 		cnt++;
    279 		flush(fswritefd, bp);
    280 		nbp = bp->b_prev;
    281 		free(bp->b_un.b_buf);
    282 		free((char *)bp);
    283 	}
    284 	if (bufhead.b_size != cnt)
    285 		errexit("Panic: lost %d buffers\n", bufhead.b_size - cnt);
    286 	pbp = pdirbp = (struct bufarea *)0;
    287 #if 0 /* FFS */
    288 	if (markclean && (sblock.lfs_clean & FS_ISCLEAN) == 0) {
    289 		/*
    290 		 * Mark the file system as clean, and sync the superblock.
    291 		 */
    292 		if (preen)
    293 			pwarn("MARKING FILE SYSTEM CLEAN\n");
    294 		else if (!reply("MARK FILE SYSTEM CLEAN"))
    295 			markclean = 0;
    296 		if (markclean) {
    297 			sblock.lfs_clean = FS_ISCLEAN;
    298 			sbdirty();
    299 			flush(fswritefd, &sblk);
    300 		}
    301 	}
    302 #endif
    303 	if (debug)
    304 		printf("cache missed %ld of %ld (%d%%)\n", diskreads,
    305 		    totalreads, (int)(diskreads * 100 / totalreads));
    306 	(void)close(fsreadfd);
    307 	(void)close(fswritefd);
    308 }
    309 
    310 int
    311 bread(fd, buf, blk, size)
    312 	int fd;
    313 	char *buf;
    314 	daddr_t blk;
    315 	long size;
    316 {
    317 	char *cp;
    318 	int i, errs;
    319 	off_t offset;
    320 
    321 	offset = blk;
    322 	offset *= dev_bsize;
    323 	if (lseek(fd, offset, 0) < 0) {
    324 		rwerror("SEEK", blk);
    325 		raise(1);
    326 	}
    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(fd, buf, blk, size)
    353 	int fd;
    354 	char *buf;
    355 	daddr_t blk;
    356 	long size;
    357 {
    358 	int i;
    359 	char *cp;
    360 	off_t offset;
    361 
    362 	if (fd < 0)
    363 		return;
    364 	offset = blk;
    365 	offset *= dev_bsize;
    366 	if (lseek(fd, offset, 0) < 0)
    367 		rwerror("SEEK", blk);
    368 	else if (write(fd, buf, (int)size) == size) {
    369 		fsmodified = 1;
    370 		return;
    371 	}
    372 	rwerror("WRITE", blk);
    373 	if (lseek(fd, offset, 0) < 0)
    374 		rwerror("SEEK", blk);
    375 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
    376 	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
    377 		if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
    378 			(void)lseek(fd, offset + i + dev_bsize, 0);
    379 			printf(" %ld,", blk + i / dev_bsize);
    380 		}
    381 	printf("\n");
    382 	return;
    383 }
    384 
    385 /*
    386  * allocate a data block with the specified number of fragments
    387  */
    388 int
    389 allocblk(frags)
    390 	long frags;
    391 {
    392 	register int i, j, k;
    393 
    394 	if (frags <= 0 || frags > sblock.lfs_frag)
    395 		return (0);
    396 	for (i = 0; i < maxfsblock - sblock.lfs_frag; i += sblock.lfs_frag) {
    397 		for (j = 0; j <= sblock.lfs_frag - frags; j++) {
    398 			if (testbmap(i + j))
    399 				continue;
    400 			for (k = 1; k < frags; k++)
    401 				if (testbmap(i + j + k))
    402 					break;
    403 			if (k < frags) {
    404 				j += k;
    405 				continue;
    406 			}
    407 			for (k = 0; k < frags; k++) {
    408 #ifndef VERBOSE_BLOCKMAP
    409                             setbmap(i + j + k);
    410 #else
    411                             setbmap(i + j + k, -1);
    412 #endif
    413                         }
    414 			n_blks += frags;
    415 			return (i + j);
    416 		}
    417 	}
    418 	return (0);
    419 }
    420 
    421 /*
    422  * Free a previously allocated block
    423  */
    424 void
    425 freeblk(blkno, frags)
    426 	daddr_t blkno;
    427 	long frags;
    428 {
    429 	struct inodesc idesc;
    430 
    431 	idesc.id_blkno = blkno;
    432 	idesc.id_numfrags = frags;
    433 	(void)pass4check(&idesc);
    434 }
    435 
    436 /*
    437  * Find a pathname
    438  */
    439 void
    440 getpathname(namebuf, curdir, ino)
    441 	char *namebuf;
    442 	ino_t curdir, ino;
    443 {
    444 	int len;
    445 	register char *cp;
    446 	struct inodesc idesc;
    447 	static int busy = 0;
    448 
    449 	if (curdir == ino && ino == ROOTINO) {
    450 		(void)strcpy(namebuf, "/");
    451 		return;
    452 	}
    453 	if (busy ||
    454 	    (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
    455 		(void)strcpy(namebuf, "?");
    456 		return;
    457 	}
    458 	busy = 1;
    459 	memset(&idesc, 0, sizeof(struct inodesc));
    460 	idesc.id_type = DATA;
    461 	idesc.id_fix = IGNORE;
    462 	cp = &namebuf[MAXPATHLEN - 1];
    463 	*cp = '\0';
    464 	if (curdir != ino) {
    465 		idesc.id_parent = curdir;
    466 		goto namelookup;
    467 	}
    468 	while (ino != ROOTINO) {
    469 		idesc.id_number = ino;
    470 		idesc.id_func = findino;
    471 		idesc.id_name = "..";
    472 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
    473 			break;
    474 	namelookup:
    475 		idesc.id_number = idesc.id_parent;
    476 		idesc.id_parent = ino;
    477 		idesc.id_func = findname;
    478 		idesc.id_name = namebuf;
    479 		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
    480 			break;
    481 		len = strlen(namebuf);
    482 		cp -= len;
    483 		memcpy(cp, namebuf, (size_t)len);
    484 		*--cp = '/';
    485 		if (cp < &namebuf[MAXNAMLEN])
    486 			break;
    487 		ino = idesc.id_number;
    488 	}
    489 	busy = 0;
    490 	if (ino != ROOTINO)
    491 		*--cp = '?';
    492 	memcpy(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
    493 }
    494 
    495 void
    496 catch(n)
    497 	int n;
    498 {
    499 	if (!doinglevel2)
    500 		ckfini(0);
    501 	exit(12);
    502 }
    503 
    504 /*
    505  * When preening, allow a single quit to signal
    506  * a special exit after filesystem checks complete
    507  * so that reboot sequence may be interrupted.
    508  */
    509 void
    510 catchquit(n)
    511 	int n;
    512 {
    513 	extern int returntosingle;
    514 
    515 	printf("returning to single-user after filesystem check\n");
    516 	returntosingle = 1;
    517 	(void)signal(SIGQUIT, SIG_DFL);
    518 }
    519 
    520 /*
    521  * Ignore a single quit signal; wait and flush just in case.
    522  * Used by child processes in preen.
    523  */
    524 void
    525 voidquit(n)
    526 	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(idesc, msg)
    539 	register struct inodesc *idesc;
    540 	char *msg;
    541 {
    542 
    543 	switch (idesc->id_fix) {
    544 
    545 	case DONTKNOW:
    546 		if (idesc->id_type == DATA)
    547 			direrror(idesc->id_number, msg);
    548 		else
    549 			pwarn(msg);
    550 		if (preen) {
    551 			printf(" (SALVAGED)\n");
    552 			idesc->id_fix = FIX;
    553 			return (ALTERED);
    554 		}
    555 		if (reply("SALVAGE") == 0) {
    556 			idesc->id_fix = NOFIX;
    557 			return (0);
    558 		}
    559 		idesc->id_fix = FIX;
    560 		return (ALTERED);
    561 
    562 	case FIX:
    563 		return (ALTERED);
    564 
    565 	case NOFIX:
    566 	case IGNORE:
    567 		return (0);
    568 
    569 	default:
    570 		errexit("UNKNOWN INODESC FIX MODE %d\n", idesc->id_fix);
    571 	}
    572 	/* NOTREACHED */
    573 }
    574