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