Home | History | Annotate | Line # | Download | only in fsck_lfs
utilities.c revision 1.7
      1  1.7  christos /* $NetBSD: utilities.c,v 1.7 2001/02/04 21:52:04 christos 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.7  christos extern int      returntosingle;
     59  1.7  christos 
     60  1.1  perseant int
     61  1.4  perseant ftypeok(struct dinode * dp)
     62  1.1  perseant {
     63  1.1  perseant 	switch (dp->di_mode & IFMT) {
     64  1.1  perseant 
     65  1.4  perseant 		case IFDIR:
     66  1.4  perseant 		case IFREG:
     67  1.4  perseant 		case IFBLK:
     68  1.4  perseant 		case IFCHR:
     69  1.4  perseant 		case IFLNK:
     70  1.4  perseant 		case IFSOCK:
     71  1.4  perseant 		case IFIFO:
     72  1.1  perseant 		return (1);
     73  1.1  perseant 
     74  1.1  perseant 	default:
     75  1.1  perseant 		if (debug)
     76  1.1  perseant 			printf("bad file type 0%o\n", dp->di_mode);
     77  1.1  perseant 		return (0);
     78  1.1  perseant 	}
     79  1.1  perseant }
     80  1.1  perseant 
     81  1.1  perseant int
     82  1.4  perseant reply(char *question)
     83  1.1  perseant {
     84  1.4  perseant 	int             persevere;
     85  1.4  perseant 	char            c;
     86  1.1  perseant 
     87  1.1  perseant 	if (preen)
     88  1.1  perseant 		pfatal("INTERNAL ERROR: GOT TO reply()");
     89  1.1  perseant 	persevere = !strcmp(question, "CONTINUE");
     90  1.1  perseant 	printf("\n");
     91  1.1  perseant 	if (!persevere && (nflag || fswritefd < 0)) {
     92  1.1  perseant 		printf("%s? no\n\n", question);
     93  1.1  perseant 		return (0);
     94  1.1  perseant 	}
     95  1.1  perseant 	if (yflag || (persevere && nflag)) {
     96  1.1  perseant 		printf("%s? yes\n\n", question);
     97  1.1  perseant 		return (1);
     98  1.1  perseant 	}
     99  1.4  perseant 	do {
    100  1.1  perseant 		printf("%s? [yn] ", question);
    101  1.4  perseant 		(void)fflush(stdout);
    102  1.1  perseant 		c = getc(stdin);
    103  1.1  perseant 		while (c != '\n' && getc(stdin) != '\n')
    104  1.1  perseant 			if (feof(stdin))
    105  1.1  perseant 				return (0);
    106  1.1  perseant 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
    107  1.1  perseant 	printf("\n");
    108  1.1  perseant 	if (c == 'y' || c == 'Y')
    109  1.1  perseant 		return (1);
    110  1.1  perseant 	return (0);
    111  1.1  perseant }
    112  1.1  perseant 
    113  1.1  perseant /*
    114  1.1  perseant  * Malloc buffers and set up cache.
    115  1.1  perseant  */
    116  1.1  perseant void
    117  1.1  perseant bufinit()
    118  1.1  perseant {
    119  1.1  perseant 	register struct bufarea *bp;
    120  1.4  perseant 	long            bufcnt, i;
    121  1.4  perseant 	char           *bufp;
    122  1.1  perseant 
    123  1.1  perseant 	pbp = pdirbp = (struct bufarea *)0;
    124  1.1  perseant 	bufp = malloc((unsigned int)sblock.lfs_bsize);
    125  1.1  perseant 	if (bufp == 0)
    126  1.1  perseant 		errexit("cannot allocate buffer pool\n");
    127  1.1  perseant 	/* cgblk.b_un.b_buf = bufp; */
    128  1.1  perseant 	/* initbarea(&cgblk); */
    129  1.1  perseant 	bufhead.b_next = bufhead.b_prev = &bufhead;
    130  1.1  perseant 	bufcnt = MAXBUFSPACE / sblock.lfs_bsize;
    131  1.1  perseant 	if (bufcnt < MINBUFS)
    132  1.1  perseant 		bufcnt = MINBUFS;
    133  1.1  perseant 	for (i = 0; i < bufcnt; i++) {
    134  1.1  perseant 		bp = (struct bufarea *)malloc(sizeof(struct bufarea));
    135  1.1  perseant 		bufp = malloc((unsigned int)sblock.lfs_bsize);
    136  1.1  perseant 		if (bp == NULL || bufp == NULL) {
    137  1.1  perseant 			if (i >= MINBUFS)
    138  1.1  perseant 				break;
    139  1.1  perseant 			errexit("cannot allocate buffer pool\n");
    140  1.1  perseant 		}
    141  1.1  perseant 		bp->b_un.b_buf = bufp;
    142  1.1  perseant 		bp->b_prev = &bufhead;
    143  1.1  perseant 		bp->b_next = bufhead.b_next;
    144  1.1  perseant 		bufhead.b_next->b_prev = bp;
    145  1.1  perseant 		bufhead.b_next = bp;
    146  1.1  perseant 		initbarea(bp);
    147  1.1  perseant 	}
    148  1.1  perseant 	bufhead.b_size = i;	/* save number of buffers */
    149  1.1  perseant }
    150  1.1  perseant 
    151  1.1  perseant /*
    152  1.1  perseant  * Manage a cache of directory blocks.
    153  1.1  perseant  */
    154  1.1  perseant struct bufarea *
    155  1.4  perseant getddblk(daddr_t blkno, long size)
    156  1.1  perseant {
    157  1.1  perseant 	register struct bufarea *bp;
    158  1.1  perseant 
    159  1.1  perseant 	for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next)
    160  1.1  perseant 		if (bp->b_bno == blkno) {
    161  1.4  perseant 			if (bp->b_size <= size)
    162  1.4  perseant 				getdblk(bp, blkno, size);
    163  1.4  perseant 			goto foundit;
    164  1.4  perseant 		}
    165  1.1  perseant 	for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev)
    166  1.1  perseant 		if ((bp->b_flags & B_INUSE) == 0)
    167  1.1  perseant 			break;
    168  1.1  perseant 	if (bp == &bufhead)
    169  1.1  perseant 		errexit("deadlocked buffer pool\n");
    170  1.1  perseant 	getdblk(bp, blkno, size);
    171  1.1  perseant 	/* fall through */
    172  1.1  perseant foundit:
    173  1.1  perseant 	totalreads++;
    174  1.1  perseant 	bp->b_prev->b_next = bp->b_next;
    175  1.1  perseant 	bp->b_next->b_prev = bp->b_prev;
    176  1.1  perseant 	bp->b_prev = &bufhead;
    177  1.1  perseant 	bp->b_next = bufhead.b_next;
    178  1.1  perseant 	bufhead.b_next->b_prev = bp;
    179  1.1  perseant 	bufhead.b_next = bp;
    180  1.1  perseant 	bp->b_flags |= B_INUSE;
    181  1.1  perseant 	return (bp);
    182  1.1  perseant }
    183  1.1  perseant 
    184  1.1  perseant struct bufarea *
    185  1.4  perseant getdatablk(daddr_t blkno, long size)
    186  1.1  perseant {
    187  1.4  perseant 	return getddblk(fsbtodb(&sblock, blkno), size);
    188  1.1  perseant }
    189  1.1  perseant 
    190  1.4  perseant void
    191  1.4  perseant getdblk(struct bufarea * bp, daddr_t blk, long size)
    192  1.1  perseant {
    193  1.1  perseant 	if (bp->b_bno != blk) {
    194  1.1  perseant 		flush(fswritefd, bp);
    195  1.1  perseant 		diskreads++;
    196  1.1  perseant 		bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, blk, size);
    197  1.1  perseant 		bp->b_bno = blk;
    198  1.1  perseant 		bp->b_size = size;
    199  1.1  perseant 	}
    200  1.1  perseant }
    201  1.1  perseant 
    202  1.1  perseant void
    203  1.4  perseant getblk(struct bufarea * bp, daddr_t blk, long size)
    204  1.1  perseant {
    205  1.4  perseant 	getdblk(bp, fsbtodb(&sblock, blk), size);
    206  1.1  perseant }
    207  1.1  perseant 
    208  1.1  perseant void
    209  1.4  perseant flush(int fd, struct bufarea * bp)
    210  1.1  perseant {
    211  1.1  perseant 	if (!bp->b_dirty)
    212  1.1  perseant 		return;
    213  1.1  perseant 	if (bp->b_errs != 0)
    214  1.1  perseant 		pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n",
    215  1.4  perseant 		 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
    216  1.4  perseant 		       bp->b_bno);
    217  1.1  perseant 	bp->b_dirty = 0;
    218  1.1  perseant 	bp->b_errs = 0;
    219  1.1  perseant 	bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size);
    220  1.1  perseant 	if (bp != &sblk)
    221  1.1  perseant 		return;
    222  1.4  perseant #if 0				/* XXX - FFS */
    223  1.1  perseant 	for (i = 0, j = 0; i < sblock.lfs_cssize; i += sblock.lfs_bsize, j++) {
    224  1.1  perseant 		bwrite(fswritefd, (char *)sblock.lfs_csp[j],
    225  1.4  perseant 		  fsbtodb(&sblock, sblock.lfs_csaddr + j * sblock.lfs_frag),
    226  1.4  perseant 		       sblock.lfs_cssize - i < sblock.lfs_bsize ?
    227  1.4  perseant 		       sblock.lfs_cssize - i : sblock.lfs_bsize);
    228  1.1  perseant 	}
    229  1.1  perseant #endif
    230  1.1  perseant }
    231  1.1  perseant 
    232  1.1  perseant static void
    233  1.4  perseant rwerror(char *mesg, daddr_t blk)
    234  1.1  perseant {
    235  1.1  perseant 
    236  1.1  perseant 	if (preen == 0)
    237  1.1  perseant 		printf("\n");
    238  1.1  perseant 	pfatal("CANNOT %s: BLK %d", mesg, blk);
    239  1.1  perseant 	if (reply("CONTINUE") == 0)
    240  1.1  perseant 		errexit("Program terminated\n");
    241  1.1  perseant }
    242  1.1  perseant 
    243  1.1  perseant void
    244  1.4  perseant ckfini(int markclean)
    245  1.1  perseant {
    246  1.1  perseant 	register struct bufarea *bp, *nbp;
    247  1.4  perseant 	int             cnt = 0;
    248  1.1  perseant 
    249  1.1  perseant 	if (fswritefd < 0) {
    250  1.1  perseant 		(void)close(fsreadfd);
    251  1.1  perseant 		return;
    252  1.1  perseant 	}
    253  1.1  perseant 	flush(fswritefd, &sblk);
    254  1.3  perseant 	if (havesb && sblk.b_bno != LFS_LABELPAD / dev_bsize &&
    255  1.3  perseant 	    sblk.b_bno != sblock.lfs_sboffs[0] &&
    256  1.3  perseant 	    !preen && reply("UPDATE STANDARD SUPERBLOCKS")) {
    257  1.3  perseant 		sblk.b_bno = LFS_LABELPAD / dev_bsize;
    258  1.1  perseant 		sbdirty();
    259  1.1  perseant 		flush(fswritefd, &sblk);
    260  1.1  perseant 	}
    261  1.3  perseant 	if (havesb) {
    262  1.4  perseant 		if (sblk.b_bno == LFS_LABELPAD / dev_bsize) {
    263  1.3  perseant 			/* Do the first alternate */
    264  1.3  perseant 			sblk.b_bno = sblock.lfs_sboffs[1];
    265  1.3  perseant 			sbdirty();
    266  1.3  perseant 			flush(fswritefd, &sblk);
    267  1.4  perseant 		} else if (sblk.b_bno == sblock.lfs_sboffs[1]) {
    268  1.3  perseant 			/* Do the primary */
    269  1.3  perseant 			sblk.b_bno = LFS_LABELPAD / dev_bsize;
    270  1.3  perseant 			sbdirty();
    271  1.3  perseant 			flush(fswritefd, &sblk);
    272  1.3  perseant 		}
    273  1.3  perseant 	}
    274  1.1  perseant 	/* flush(fswritefd, &cgblk); */
    275  1.1  perseant 	/* free(cgblk.b_un.b_buf); */
    276  1.1  perseant 	for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) {
    277  1.1  perseant 		cnt++;
    278  1.1  perseant 		flush(fswritefd, bp);
    279  1.1  perseant 		nbp = bp->b_prev;
    280  1.1  perseant 		free(bp->b_un.b_buf);
    281  1.1  perseant 		free((char *)bp);
    282  1.1  perseant 	}
    283  1.1  perseant 	if (bufhead.b_size != cnt)
    284  1.1  perseant 		errexit("Panic: lost %d buffers\n", bufhead.b_size - cnt);
    285  1.1  perseant 	pbp = pdirbp = (struct bufarea *)0;
    286  1.3  perseant 	if (markclean && sblock.lfs_clean == 0) {
    287  1.1  perseant 		/*
    288  1.1  perseant 		 * Mark the file system as clean, and sync the superblock.
    289  1.1  perseant 		 */
    290  1.1  perseant 		if (preen)
    291  1.1  perseant 			pwarn("MARKING FILE SYSTEM CLEAN\n");
    292  1.1  perseant 		else if (!reply("MARK FILE SYSTEM CLEAN"))
    293  1.1  perseant 			markclean = 0;
    294  1.1  perseant 		if (markclean) {
    295  1.3  perseant 			sblock.lfs_clean = 1;
    296  1.1  perseant 			sbdirty();
    297  1.1  perseant 			flush(fswritefd, &sblk);
    298  1.4  perseant 			if (sblk.b_bno == LFS_LABELPAD / dev_bsize) {
    299  1.3  perseant 				/* Do the first alternate */
    300  1.3  perseant 				sblk.b_bno = sblock.lfs_sboffs[0];
    301  1.3  perseant 				flush(fswritefd, &sblk);
    302  1.4  perseant 			} else if (sblk.b_bno == sblock.lfs_sboffs[0]) {
    303  1.3  perseant 				/* Do the primary */
    304  1.3  perseant 				sblk.b_bno = LFS_LABELPAD / dev_bsize;
    305  1.3  perseant 				flush(fswritefd, &sblk);
    306  1.3  perseant 			}
    307  1.1  perseant 		}
    308  1.1  perseant 	}
    309  1.1  perseant 	if (debug)
    310  1.1  perseant 		printf("cache missed %ld of %ld (%d%%)\n", diskreads,
    311  1.4  perseant 		       totalreads, (int)(diskreads * 100 / totalreads));
    312  1.1  perseant 	(void)close(fsreadfd);
    313  1.1  perseant 	(void)close(fswritefd);
    314  1.1  perseant }
    315  1.1  perseant 
    316  1.1  perseant int
    317  1.4  perseant bread(int fd, char *buf, daddr_t blk, long size)
    318  1.4  perseant {
    319  1.4  perseant 	char           *cp;
    320  1.4  perseant 	int             i, errs;
    321  1.4  perseant 	off_t           offset;
    322  1.1  perseant 
    323  1.1  perseant 	offset = blk;
    324  1.1  perseant 	offset *= dev_bsize;
    325  1.1  perseant 	if (lseek(fd, offset, 0) < 0) {
    326  1.1  perseant 		rwerror("SEEK", blk);
    327  1.4  perseant 	} else if (read(fd, buf, (int)size) == size)
    328  1.1  perseant 		return (0);
    329  1.1  perseant 	rwerror("READ", blk);
    330  1.1  perseant 	if (lseek(fd, offset, 0) < 0)
    331  1.1  perseant 		rwerror("SEEK", blk);
    332  1.1  perseant 	errs = 0;
    333  1.1  perseant 	memset(buf, 0, (size_t)size);
    334  1.1  perseant 	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
    335  1.1  perseant 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
    336  1.1  perseant 		if (read(fd, cp, (int)secsize) != secsize) {
    337  1.1  perseant 			(void)lseek(fd, offset + i + secsize, 0);
    338  1.1  perseant 			if (secsize != dev_bsize && dev_bsize != 1)
    339  1.1  perseant 				printf(" %ld (%ld),",
    340  1.4  perseant 				       (blk * dev_bsize + i) / secsize,
    341  1.4  perseant 				       blk + i / dev_bsize);
    342  1.1  perseant 			else
    343  1.1  perseant 				printf(" %ld,", blk + i / dev_bsize);
    344  1.1  perseant 			errs++;
    345  1.1  perseant 		}
    346  1.1  perseant 	}
    347  1.1  perseant 	printf("\n");
    348  1.1  perseant 	return (errs);
    349  1.1  perseant }
    350  1.1  perseant 
    351  1.1  perseant void
    352  1.4  perseant bwrite(int fd, char *buf, daddr_t blk, long size)
    353  1.4  perseant {
    354  1.4  perseant 	int             i;
    355  1.4  perseant 	char           *cp;
    356  1.4  perseant 	off_t           offset;
    357  1.1  perseant 
    358  1.1  perseant 	if (fd < 0)
    359  1.1  perseant 		return;
    360  1.1  perseant 	offset = blk;
    361  1.1  perseant 	offset *= dev_bsize;
    362  1.1  perseant 	if (lseek(fd, offset, 0) < 0)
    363  1.1  perseant 		rwerror("SEEK", blk);
    364  1.1  perseant 	else if (write(fd, buf, (int)size) == size) {
    365  1.1  perseant 		fsmodified = 1;
    366  1.1  perseant 		return;
    367  1.1  perseant 	}
    368  1.1  perseant 	rwerror("WRITE", blk);
    369  1.1  perseant 	if (lseek(fd, offset, 0) < 0)
    370  1.1  perseant 		rwerror("SEEK", blk);
    371  1.1  perseant 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
    372  1.1  perseant 	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
    373  1.1  perseant 		if (write(fd, cp, (int)dev_bsize) != dev_bsize) {
    374  1.1  perseant 			(void)lseek(fd, offset + i + dev_bsize, 0);
    375  1.1  perseant 			printf(" %ld,", blk + i / dev_bsize);
    376  1.1  perseant 		}
    377  1.1  perseant 	printf("\n");
    378  1.1  perseant 	return;
    379  1.1  perseant }
    380  1.1  perseant 
    381  1.1  perseant /*
    382  1.1  perseant  * allocate a data block with the specified number of fragments
    383  1.1  perseant  */
    384  1.1  perseant int
    385  1.4  perseant allocblk(long frags)
    386  1.1  perseant {
    387  1.3  perseant #if 1
    388  1.3  perseant 	/*
    389  1.3  perseant 	 * XXX Can't allocate blocks right now because we would have to do
    390  1.3  perseant 	 * a full partial segment write.
    391  1.3  perseant 	 */
    392  1.3  perseant 	return 0;
    393  1.4  perseant #else				/* 0 */
    394  1.4  perseant 	register int    i, j, k;
    395  1.1  perseant 
    396  1.1  perseant 	if (frags <= 0 || frags > sblock.lfs_frag)
    397  1.1  perseant 		return (0);
    398  1.1  perseant 	for (i = 0; i < maxfsblock - sblock.lfs_frag; i += sblock.lfs_frag) {
    399  1.1  perseant 		for (j = 0; j <= sblock.lfs_frag - frags; j++) {
    400  1.1  perseant 			if (testbmap(i + j))
    401  1.1  perseant 				continue;
    402  1.1  perseant 			for (k = 1; k < frags; k++)
    403  1.1  perseant 				if (testbmap(i + j + k))
    404  1.1  perseant 					break;
    405  1.1  perseant 			if (k < frags) {
    406  1.1  perseant 				j += k;
    407  1.1  perseant 				continue;
    408  1.1  perseant 			}
    409  1.1  perseant 			for (k = 0; k < frags; k++) {
    410  1.1  perseant #ifndef VERBOSE_BLOCKMAP
    411  1.4  perseant 				setbmap(i + j + k);
    412  1.1  perseant #else
    413  1.4  perseant 				setbmap(i + j + k, -1);
    414  1.1  perseant #endif
    415  1.4  perseant 			}
    416  1.1  perseant 			n_blks += frags;
    417  1.1  perseant 			return (i + j);
    418  1.1  perseant 		}
    419  1.1  perseant 	}
    420  1.1  perseant 	return (0);
    421  1.4  perseant #endif				/* 0 */
    422  1.1  perseant }
    423  1.1  perseant 
    424  1.1  perseant /*
    425  1.1  perseant  * Free a previously allocated block
    426  1.1  perseant  */
    427  1.1  perseant void
    428  1.4  perseant freeblk(daddr_t blkno, long frags)
    429  1.1  perseant {
    430  1.4  perseant 	struct inodesc  idesc;
    431  1.1  perseant 
    432  1.1  perseant 	idesc.id_blkno = blkno;
    433  1.1  perseant 	idesc.id_numfrags = frags;
    434  1.1  perseant 	(void)pass4check(&idesc);
    435  1.1  perseant }
    436  1.1  perseant 
    437  1.1  perseant /*
    438  1.1  perseant  * Find a pathname
    439  1.1  perseant  */
    440  1.1  perseant void
    441  1.4  perseant getpathname(char *namebuf, ino_t curdir, ino_t ino)
    442  1.4  perseant {
    443  1.4  perseant 	int             len;
    444  1.4  perseant 	register char  *cp;
    445  1.4  perseant 	struct inodesc  idesc;
    446  1.4  perseant 	static int      busy = 0;
    447  1.1  perseant 
    448  1.1  perseant 	if (curdir == ino && ino == ROOTINO) {
    449  1.1  perseant 		(void)strcpy(namebuf, "/");
    450  1.1  perseant 		return;
    451  1.1  perseant 	}
    452  1.1  perseant 	if (busy ||
    453  1.1  perseant 	    (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) {
    454  1.1  perseant 		(void)strcpy(namebuf, "?");
    455  1.1  perseant 		return;
    456  1.1  perseant 	}
    457  1.1  perseant 	busy = 1;
    458  1.1  perseant 	memset(&idesc, 0, sizeof(struct inodesc));
    459  1.1  perseant 	idesc.id_type = DATA;
    460  1.1  perseant 	idesc.id_fix = IGNORE;
    461  1.1  perseant 	cp = &namebuf[MAXPATHLEN - 1];
    462  1.1  perseant 	*cp = '\0';
    463  1.1  perseant 	if (curdir != ino) {
    464  1.1  perseant 		idesc.id_parent = curdir;
    465  1.1  perseant 		goto namelookup;
    466  1.1  perseant 	}
    467  1.1  perseant 	while (ino != ROOTINO) {
    468  1.1  perseant 		idesc.id_number = ino;
    469  1.1  perseant 		idesc.id_func = findino;
    470  1.1  perseant 		idesc.id_name = "..";
    471  1.1  perseant 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
    472  1.1  perseant 			break;
    473  1.4  perseant namelookup:
    474  1.1  perseant 		idesc.id_number = idesc.id_parent;
    475  1.1  perseant 		idesc.id_parent = ino;
    476  1.1  perseant 		idesc.id_func = findname;
    477  1.1  perseant 		idesc.id_name = namebuf;
    478  1.4  perseant 		if ((ckinode(ginode(idesc.id_number), &idesc) & FOUND) == 0)
    479  1.1  perseant 			break;
    480  1.1  perseant 		len = strlen(namebuf);
    481  1.1  perseant 		cp -= len;
    482  1.1  perseant 		memcpy(cp, namebuf, (size_t)len);
    483  1.1  perseant 		*--cp = '/';
    484  1.1  perseant 		if (cp < &namebuf[MAXNAMLEN])
    485  1.1  perseant 			break;
    486  1.1  perseant 		ino = idesc.id_number;
    487  1.1  perseant 	}
    488  1.1  perseant 	busy = 0;
    489  1.1  perseant 	if (ino != ROOTINO)
    490  1.1  perseant 		*--cp = '?';
    491  1.1  perseant 	memcpy(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
    492  1.1  perseant }
    493  1.1  perseant 
    494  1.1  perseant void
    495  1.4  perseant catch(int n)
    496  1.1  perseant {
    497  1.1  perseant 	if (!doinglevel2)
    498  1.1  perseant 		ckfini(0);
    499  1.1  perseant 	exit(12);
    500  1.1  perseant }
    501  1.1  perseant 
    502  1.1  perseant /*
    503  1.1  perseant  * When preening, allow a single quit to signal
    504  1.1  perseant  * a special exit after filesystem checks complete
    505  1.1  perseant  * so that reboot sequence may be interrupted.
    506  1.1  perseant  */
    507  1.1  perseant void
    508  1.4  perseant catchquit(int n)
    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.6        is 			pwarn("%s", 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