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