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