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