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