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