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