xref: /freebsd/sbin/fsck_ffs/fsutil.c (revision b241767f8ef38f9ca7c109fe2fccd11ccbfaa4f0)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44336716bSAdrian Chadd  * Copyright (c) 1980, 1986, 1993
54336716bSAdrian Chadd  *	The Regents of the University of California.  All rights reserved.
64336716bSAdrian Chadd  *
74336716bSAdrian Chadd  * Redistribution and use in source and binary forms, with or without
84336716bSAdrian Chadd  * modification, are permitted provided that the following conditions
94336716bSAdrian Chadd  * are met:
104336716bSAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
114336716bSAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
124336716bSAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
134336716bSAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
144336716bSAdrian Chadd  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
164336716bSAdrian Chadd  *    may be used to endorse or promote products derived from this software
174336716bSAdrian Chadd  *    without specific prior written permission.
184336716bSAdrian Chadd  *
194336716bSAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
204336716bSAdrian Chadd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214336716bSAdrian Chadd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224336716bSAdrian Chadd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
234336716bSAdrian Chadd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
244336716bSAdrian Chadd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
254336716bSAdrian Chadd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264336716bSAdrian Chadd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274336716bSAdrian Chadd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284336716bSAdrian Chadd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294336716bSAdrian Chadd  * SUCH DAMAGE.
304336716bSAdrian Chadd  */
314336716bSAdrian Chadd 
324336716bSAdrian Chadd #include <sys/param.h>
339d580d7cSIan Dowse #include <sys/time.h>
344336716bSAdrian Chadd #include <sys/types.h>
35bf58d635SIan Dowse #include <sys/sysctl.h>
368d3dfc26SDag-Erling Smørgrav #include <sys/disk.h>
371c85e6a3SKirk McKusick #include <sys/disklabel.h>
388d3dfc26SDag-Erling Smørgrav #include <sys/ioctl.h>
394336716bSAdrian Chadd #include <sys/stat.h>
404336716bSAdrian Chadd 
414336716bSAdrian Chadd #include <ufs/ufs/dinode.h>
424336716bSAdrian Chadd #include <ufs/ufs/dir.h>
434336716bSAdrian Chadd #include <ufs/ffs/fs.h>
444336716bSAdrian Chadd 
454336716bSAdrian Chadd #include <err.h>
464336716bSAdrian Chadd #include <errno.h>
474336716bSAdrian Chadd #include <string.h>
484336716bSAdrian Chadd #include <ctype.h>
494336716bSAdrian Chadd #include <fstab.h>
5084fc0d7eSMaxime Henrion #include <stdint.h>
514336716bSAdrian Chadd #include <stdio.h>
524336716bSAdrian Chadd #include <stdlib.h>
53ed75b5a1SKirk McKusick #include <time.h>
544336716bSAdrian Chadd #include <unistd.h>
554336716bSAdrian Chadd 
564336716bSAdrian Chadd #include "fsck.h"
574336716bSAdrian Chadd 
585cc52631SKirk McKusick int		sujrecovery = 0;
595cc52631SKirk McKusick 
605cc52631SKirk McKusick static struct bufarea *allocbuf(const char *);
615cc52631SKirk McKusick static void cg_write(struct bufarea *);
629d580d7cSIan Dowse static void slowio_start(void);
639d580d7cSIan Dowse static void slowio_end(void);
64ed75b5a1SKirk McKusick static void printIOstats(void);
659d580d7cSIan Dowse 
66ed75b5a1SKirk McKusick static long diskreads, totaldiskreads, totalreads; /* Disk cache statistics */
67ed75b5a1SKirk McKusick static struct timespec startpass, finishpass;
689d580d7cSIan Dowse struct timeval slowio_starttime;
699d580d7cSIan Dowse int slowio_delay_usec = 10000;	/* Initial IO delay for background fsck */
709d580d7cSIan Dowse int slowio_pollcnt;
7181fbded2SKirk McKusick static struct bufarea cgblk;	/* backup buffer for cylinder group blocks */
72460ed610SKirk McKusick static struct bufarea failedbuf; /* returned by failed getdatablk() */
735cc52631SKirk McKusick static TAILQ_HEAD(bufqueue, bufarea) bufqueuehd; /* head of buffer cache LRU */
745cc52631SKirk McKusick static LIST_HEAD(bufhash, bufarea) bufhashhd[HASHSIZE]; /* buffer hash list */
75460ed610SKirk McKusick static struct bufhash freebufs;	/* unused buffers */
762ec5c914SKirk McKusick static int numbufs;		/* size of buffer cache */
775cc52631SKirk McKusick static int cachelookups;	/* number of cache lookups */
785cc52631SKirk McKusick static int cachereads;		/* number of cache reads */
797703a6ffSScott Long static int flushtries;		/* number of tries to reclaim memory */
807703a6ffSScott Long 
815cc52631SKirk McKusick char *buftype[BT_NUMBUFTYPES] = BT_NAMES;
825cc52631SKirk McKusick 
837703a6ffSScott Long void
fsutilinit(void)847703a6ffSScott Long fsutilinit(void)
857703a6ffSScott Long {
867703a6ffSScott Long 	diskreads = totaldiskreads = totalreads = 0;
877703a6ffSScott Long 	bzero(&startpass, sizeof(struct timespec));
887703a6ffSScott Long 	bzero(&finishpass, sizeof(struct timespec));
897703a6ffSScott Long 	bzero(&slowio_starttime, sizeof(struct timeval));
907703a6ffSScott Long 	slowio_delay_usec = 10000;
917703a6ffSScott Long 	slowio_pollcnt = 0;
927703a6ffSScott Long 	flushtries = 0;
937703a6ffSScott Long }
944336716bSAdrian Chadd 
954336716bSAdrian Chadd int
ftypeok(union dinode * dp)961c85e6a3SKirk McKusick ftypeok(union dinode *dp)
974336716bSAdrian Chadd {
98d8ba45e2SEd Maste 	switch (DIP(dp, di_mode) & IFMT) {
994336716bSAdrian Chadd 
100d8ba45e2SEd Maste 	case IFDIR:
101d8ba45e2SEd Maste 	case IFREG:
102d8ba45e2SEd Maste 	case IFBLK:
103d8ba45e2SEd Maste 	case IFCHR:
104d8ba45e2SEd Maste 	case IFLNK:
105d8ba45e2SEd Maste 	case IFSOCK:
106d8ba45e2SEd Maste 	case IFIFO:
1074336716bSAdrian Chadd 		return (1);
1084336716bSAdrian Chadd 
1094336716bSAdrian Chadd 	default:
1104336716bSAdrian Chadd 		if (debug)
1111c85e6a3SKirk McKusick 			printf("bad file type 0%o\n", DIP(dp, di_mode));
1124336716bSAdrian Chadd 		return (0);
1134336716bSAdrian Chadd 	}
1144336716bSAdrian Chadd }
1154336716bSAdrian Chadd 
1164336716bSAdrian Chadd int
reply(const char * question)117599304a4SPoul-Henning Kamp reply(const char *question)
1184336716bSAdrian Chadd {
1194336716bSAdrian Chadd 	int persevere;
1204336716bSAdrian Chadd 	char c;
1214336716bSAdrian Chadd 
1224336716bSAdrian Chadd 	if (preen)
1234336716bSAdrian Chadd 		pfatal("INTERNAL ERROR: GOT TO reply()");
1247c332e97SKirk McKusick 	persevere = strcmp(question, "CONTINUE") == 0 ||
1257c332e97SKirk McKusick 		strcmp(question, "LOOK FOR ALTERNATE SUPERBLOCKS") == 0;
1264336716bSAdrian Chadd 	printf("\n");
1277578c6abSKirk McKusick 	if (!persevere && (nflag || (fswritefd < 0 && bkgrdflag == 0))) {
1284336716bSAdrian Chadd 		printf("%s? no\n\n", question);
1294336716bSAdrian Chadd 		resolved = 0;
1304336716bSAdrian Chadd 		return (0);
1314336716bSAdrian Chadd 	}
1324336716bSAdrian Chadd 	if (yflag || (persevere && nflag)) {
1334336716bSAdrian Chadd 		printf("%s? yes\n\n", question);
1344336716bSAdrian Chadd 		return (1);
1354336716bSAdrian Chadd 	}
1364336716bSAdrian Chadd 	do	{
1374336716bSAdrian Chadd 		printf("%s? [yn] ", question);
1384336716bSAdrian Chadd 		(void) fflush(stdout);
1394336716bSAdrian Chadd 		c = getc(stdin);
1404336716bSAdrian Chadd 		while (c != '\n' && getc(stdin) != '\n') {
1414336716bSAdrian Chadd 			if (feof(stdin)) {
1424336716bSAdrian Chadd 				resolved = 0;
1434336716bSAdrian Chadd 				return (0);
1444336716bSAdrian Chadd 			}
1454336716bSAdrian Chadd 		}
1464336716bSAdrian Chadd 	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
1474336716bSAdrian Chadd 	printf("\n");
1484336716bSAdrian Chadd 	if (c == 'y' || c == 'Y')
1494336716bSAdrian Chadd 		return (1);
1504336716bSAdrian Chadd 	resolved = 0;
1514336716bSAdrian Chadd 	return (0);
1524336716bSAdrian Chadd }
1534336716bSAdrian Chadd 
1544336716bSAdrian Chadd /*
1554336716bSAdrian Chadd  * Look up state information for an inode.
1564336716bSAdrian Chadd  */
1574336716bSAdrian Chadd struct inostat *
inoinfo(ino_t inum)158b70cd7eeSWarner Losh inoinfo(ino_t inum)
1594336716bSAdrian Chadd {
16052f97104SKirk McKusick 	static struct inostat unallocated = { USTATE, 0, 0, 0 };
1614336716bSAdrian Chadd 	struct inostatlist *ilp;
1624336716bSAdrian Chadd 	int iloff;
1634336716bSAdrian Chadd 
16411ce203eSKirk McKusick 	if (inum >= maxino)
165623d7cb6SMatthew D Fleming 		errx(EEXIT, "inoinfo: inumber %ju out of range",
166623d7cb6SMatthew D Fleming 		    (uintmax_t)inum);
1674336716bSAdrian Chadd 	ilp = &inostathead[inum / sblock.fs_ipg];
1684336716bSAdrian Chadd 	iloff = inum % sblock.fs_ipg;
1694336716bSAdrian Chadd 	if (iloff >= ilp->il_numalloced)
1704336716bSAdrian Chadd 		return (&unallocated);
1714336716bSAdrian Chadd 	return (&ilp->il_stat[iloff]);
1724336716bSAdrian Chadd }
1734336716bSAdrian Chadd 
1744336716bSAdrian Chadd /*
1754336716bSAdrian Chadd  * Malloc buffers and set up cache.
1764336716bSAdrian Chadd  */
1774336716bSAdrian Chadd void
bufinit(void)178b70cd7eeSWarner Losh bufinit(void)
1794336716bSAdrian Chadd {
1805cc52631SKirk McKusick 	int i;
1814336716bSAdrian Chadd 
182460ed610SKirk McKusick 	initbarea(&failedbuf, BT_UNKNOWN);
183460ed610SKirk McKusick 	failedbuf.b_errs = -1;
184460ed610SKirk McKusick 	failedbuf.b_un.b_buf = NULL;
185*772430ddSKirk McKusick 	if ((cgblk.b_un.b_buf = Balloc((unsigned int)sblock.fs_bsize)) == NULL)
1865cc52631SKirk McKusick 		errx(EEXIT, "Initial malloc(%d) failed", sblock.fs_bsize);
187ed75b5a1SKirk McKusick 	initbarea(&cgblk, BT_CYLGRP);
1885cc52631SKirk McKusick 	numbufs = cachelookups = cachereads = 0;
1895cc52631SKirk McKusick 	TAILQ_INIT(&bufqueuehd);
190460ed610SKirk McKusick 	LIST_INIT(&freebufs);
1915cc52631SKirk McKusick 	for (i = 0; i < HASHSIZE; i++)
1925cc52631SKirk McKusick 		LIST_INIT(&bufhashhd[i]);
193ed75b5a1SKirk McKusick 	for (i = 0; i < BT_NUMBUFTYPES; i++) {
194ed75b5a1SKirk McKusick 		readtime[i].tv_sec = totalreadtime[i].tv_sec = 0;
195ed75b5a1SKirk McKusick 		readtime[i].tv_nsec = totalreadtime[i].tv_nsec = 0;
196ed75b5a1SKirk McKusick 		readcnt[i] = totalreadcnt[i] = 0;
197ed75b5a1SKirk McKusick 	}
1984336716bSAdrian Chadd }
1994336716bSAdrian Chadd 
2005cc52631SKirk McKusick static struct bufarea *
allocbuf(const char * failreason)2015cc52631SKirk McKusick allocbuf(const char *failreason)
2025cc52631SKirk McKusick {
2035cc52631SKirk McKusick 	struct bufarea *bp;
2045cc52631SKirk McKusick 	char *bufp;
2055cc52631SKirk McKusick 
2065cc52631SKirk McKusick 	bp = (struct bufarea *)Malloc(sizeof(struct bufarea));
207*772430ddSKirk McKusick 	bufp = Balloc((unsigned int)sblock.fs_bsize);
2085cc52631SKirk McKusick 	if (bp == NULL || bufp == NULL) {
2095cc52631SKirk McKusick 		errx(EEXIT, "%s", failreason);
2105cc52631SKirk McKusick 		/* NOTREACHED */
2115cc52631SKirk McKusick 	}
2125cc52631SKirk McKusick 	numbufs++;
2135cc52631SKirk McKusick 	bp->b_un.b_buf = bufp;
2145cc52631SKirk McKusick 	TAILQ_INSERT_HEAD(&bufqueuehd, bp, b_list);
2155cc52631SKirk McKusick 	initbarea(bp, BT_UNKNOWN);
2165cc52631SKirk McKusick 	return (bp);
2175cc52631SKirk McKusick }
2185cc52631SKirk McKusick 
2194336716bSAdrian Chadd /*
22081fbded2SKirk McKusick  * Manage cylinder group buffers.
22185ee267aSKirk McKusick  *
22285ee267aSKirk McKusick  * Use getblk() here rather than cgget() because the cylinder group
22385ee267aSKirk McKusick  * may be corrupted but we want it anyway so we can fix it.
22481fbded2SKirk McKusick  */
22581fbded2SKirk McKusick static struct bufarea *cgbufs;	/* header for cylinder group cache */
22681fbded2SKirk McKusick static int flushtries;		/* number of tries to reclaim memory */
22781fbded2SKirk McKusick 
22881fbded2SKirk McKusick struct bufarea *
cglookup(int cg)229957fc241SKirk McKusick cglookup(int cg)
23081fbded2SKirk McKusick {
23181fbded2SKirk McKusick 	struct bufarea *cgbp;
23281fbded2SKirk McKusick 	struct cg *cgp;
23381fbded2SKirk McKusick 
2345cc52631SKirk McKusick 	if ((unsigned) cg >= sblock.fs_ncg)
2355cc52631SKirk McKusick 		errx(EEXIT, "cglookup: out of range cylinder group %d", cg);
23681fbded2SKirk McKusick 	if (cgbufs == NULL) {
237*772430ddSKirk McKusick 		cgbufs = Calloc(sblock.fs_ncg, sizeof(struct bufarea));
23881fbded2SKirk McKusick 		if (cgbufs == NULL)
2395cc52631SKirk McKusick 			errx(EEXIT, "Cannot allocate cylinder group buffers");
24081fbded2SKirk McKusick 	}
24181fbded2SKirk McKusick 	cgbp = &cgbufs[cg];
24281fbded2SKirk McKusick 	if (cgbp->b_un.b_cg != NULL)
24381fbded2SKirk McKusick 		return (cgbp);
24481fbded2SKirk McKusick 	cgp = NULL;
24581fbded2SKirk McKusick 	if (flushtries == 0)
246*772430ddSKirk McKusick 		cgp = Balloc((unsigned int)sblock.fs_cgsize);
24781fbded2SKirk McKusick 	if (cgp == NULL) {
2485cc52631SKirk McKusick 		if (sujrecovery)
2495cc52631SKirk McKusick 			errx(EEXIT,"Ran out of memory during journal recovery");
250c0bfa109SKirk McKusick 		flush(fswritefd, &cgblk);
25181fbded2SKirk McKusick 		getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
25281fbded2SKirk McKusick 		return (&cgblk);
25381fbded2SKirk McKusick 	}
25481fbded2SKirk McKusick 	cgbp->b_un.b_cg = cgp;
25581fbded2SKirk McKusick 	initbarea(cgbp, BT_CYLGRP);
25681fbded2SKirk McKusick 	getblk(cgbp, cgtod(&sblock, cg), sblock.fs_cgsize);
25781fbded2SKirk McKusick 	return (cgbp);
25881fbded2SKirk McKusick }
25981fbded2SKirk McKusick 
26081fbded2SKirk McKusick /*
2618ebae128SKirk McKusick  * Mark a cylinder group buffer as dirty.
2628ebae128SKirk McKusick  * Update its check-hash if they are enabled.
2638ebae128SKirk McKusick  */
2648ebae128SKirk McKusick void
cgdirty(struct bufarea * cgbp)2658ebae128SKirk McKusick cgdirty(struct bufarea *cgbp)
2668ebae128SKirk McKusick {
2678ebae128SKirk McKusick 	struct cg *cg;
2688ebae128SKirk McKusick 
2698ebae128SKirk McKusick 	cg = cgbp->b_un.b_cg;
2708ebae128SKirk McKusick 	if ((sblock.fs_metackhash & CK_CYLGRP) != 0) {
2718ebae128SKirk McKusick 		cg->cg_ckhash = 0;
2728ebae128SKirk McKusick 		cg->cg_ckhash =
2738ebae128SKirk McKusick 		    calculate_crc32c(~0L, (void *)cg, sblock.fs_cgsize);
2748ebae128SKirk McKusick 	}
2758ebae128SKirk McKusick 	dirty(cgbp);
2768ebae128SKirk McKusick }
2778ebae128SKirk McKusick 
2788ebae128SKirk McKusick /*
27981fbded2SKirk McKusick  * Attempt to flush a cylinder group cache entry.
28081fbded2SKirk McKusick  * Return whether the flush was successful.
28181fbded2SKirk McKusick  */
28281fbded2SKirk McKusick int
flushentry(void)28381fbded2SKirk McKusick flushentry(void)
28481fbded2SKirk McKusick {
28581fbded2SKirk McKusick 	struct bufarea *cgbp;
28681fbded2SKirk McKusick 
2875cc52631SKirk McKusick 	if (sujrecovery || flushtries == sblock.fs_ncg || cgbufs == NULL)
288eff68496SKirk McKusick 		return (0);
28981fbded2SKirk McKusick 	cgbp = &cgbufs[flushtries++];
29081fbded2SKirk McKusick 	if (cgbp->b_un.b_cg == NULL)
29181fbded2SKirk McKusick 		return (0);
29281fbded2SKirk McKusick 	flush(fswritefd, cgbp);
29381fbded2SKirk McKusick 	free(cgbp->b_un.b_buf);
29481fbded2SKirk McKusick 	cgbp->b_un.b_buf = NULL;
29581fbded2SKirk McKusick 	return (1);
29681fbded2SKirk McKusick }
29781fbded2SKirk McKusick 
29881fbded2SKirk McKusick /*
299460ed610SKirk McKusick  * Manage a cache of filesystem disk blocks.
3004336716bSAdrian Chadd  */
3014336716bSAdrian Chadd struct bufarea *
getdatablk(ufs2_daddr_t blkno,long size,int type)302ed75b5a1SKirk McKusick getdatablk(ufs2_daddr_t blkno, long size, int type)
3034336716bSAdrian Chadd {
3043d438ad6SDavid E. O'Brien 	struct bufarea *bp;
3055cc52631SKirk McKusick 	struct bufhash *bhdp;
3064336716bSAdrian Chadd 
3075cc52631SKirk McKusick 	cachelookups++;
308460ed610SKirk McKusick 	/*
309460ed610SKirk McKusick 	 * If out of range, return empty buffer with b_err == -1
310460ed610SKirk McKusick 	 *
311460ed610SKirk McKusick 	 * Skip check for inodes because chkrange() considers
312460ed610SKirk McKusick 	 * metadata areas invalid to write data.
313460ed610SKirk McKusick 	 */
31418746531SKirk McKusick 	if (type != BT_INODES && chkrange(blkno, size / sblock.fs_fsize)) {
31518746531SKirk McKusick 		failedbuf.b_refcnt++;
316460ed610SKirk McKusick 		return (&failedbuf);
31718746531SKirk McKusick 	}
3185cc52631SKirk McKusick 	bhdp = &bufhashhd[HASH(blkno)];
3195cc52631SKirk McKusick 	LIST_FOREACH(bp, bhdp, b_hash)
3205cc52631SKirk McKusick 		if (bp->b_bno == fsbtodb(&sblock, blkno)) {
3215cc52631SKirk McKusick 			if (debug && bp->b_size != size) {
322460ed610SKirk McKusick 				prtbuf(bp, "getdatablk: size mismatch");
3235cc52631SKirk McKusick 				pfatal("getdatablk: b_size %d != size %ld\n",
3245cc52631SKirk McKusick 				    bp->b_size, size);
3255cc52631SKirk McKusick 			}
326460ed610SKirk McKusick 			TAILQ_REMOVE(&bufqueuehd, bp, b_list);
3274336716bSAdrian Chadd 			goto foundit;
3285cc52631SKirk McKusick 		}
3295cc52631SKirk McKusick 	/*
3305cc52631SKirk McKusick 	 * Move long-term busy buffer back to the front of the LRU so we
3315cc52631SKirk McKusick 	 * do not endless inspect them for recycling.
3325cc52631SKirk McKusick 	 */
3335cc52631SKirk McKusick 	bp = TAILQ_LAST(&bufqueuehd, bufqueue);
3345cc52631SKirk McKusick 	if (bp != NULL && bp->b_refcnt != 0) {
3355cc52631SKirk McKusick 		TAILQ_REMOVE(&bufqueuehd, bp, b_list);
3365cc52631SKirk McKusick 		TAILQ_INSERT_HEAD(&bufqueuehd, bp, b_list);
3375cc52631SKirk McKusick 	}
3385cc52631SKirk McKusick 	/*
3395cc52631SKirk McKusick 	 * Allocate up to the minimum number of buffers before
3405cc52631SKirk McKusick 	 * considering recycling any of them.
3415cc52631SKirk McKusick 	 */
3425cc52631SKirk McKusick 	if (size > sblock.fs_bsize)
3435cc52631SKirk McKusick 		errx(EEXIT, "Excessive buffer size %ld > %d\n", size,
3445cc52631SKirk McKusick 		    sblock.fs_bsize);
345460ed610SKirk McKusick 	if ((bp = LIST_FIRST(&freebufs)) != NULL) {
346460ed610SKirk McKusick 		LIST_REMOVE(bp, b_hash);
347460ed610SKirk McKusick 	} else if (numbufs < MINBUFS) {
3485cc52631SKirk McKusick 		bp = allocbuf("cannot create minimal buffer pool");
3495cc52631SKirk McKusick 	} else if (sujrecovery) {
3505cc52631SKirk McKusick 		/*
3515cc52631SKirk McKusick 		 * SUJ recovery does not want anything written until it
3525cc52631SKirk McKusick 		 * has successfully completed (so it can fail back to
3535cc52631SKirk McKusick 		 * full fsck). Thus, we can only recycle clean buffers.
3545cc52631SKirk McKusick 		 */
3555cc52631SKirk McKusick 		TAILQ_FOREACH_REVERSE(bp, &bufqueuehd, bufqueue, b_list)
3565cc52631SKirk McKusick 			if ((bp->b_flags & B_DIRTY) == 0 && bp->b_refcnt == 0)
3574336716bSAdrian Chadd 				break;
3582ec5c914SKirk McKusick 		if (bp == NULL)
3595cc52631SKirk McKusick 			bp = allocbuf("Ran out of memory during "
3605cc52631SKirk McKusick 			    "journal recovery");
3615cc52631SKirk McKusick 		else
3625cc52631SKirk McKusick 			LIST_REMOVE(bp, b_hash);
3635cc52631SKirk McKusick 	} else {
3645cc52631SKirk McKusick 		/*
3655cc52631SKirk McKusick 		 * Recycle oldest non-busy buffer.
3665cc52631SKirk McKusick 		 */
3675cc52631SKirk McKusick 		TAILQ_FOREACH_REVERSE(bp, &bufqueuehd, bufqueue, b_list)
3685cc52631SKirk McKusick 			if (bp->b_refcnt == 0)
3695cc52631SKirk McKusick 				break;
3705cc52631SKirk McKusick 		if (bp == NULL)
3715cc52631SKirk McKusick 			bp = allocbuf("Ran out of memory for buffers");
3725cc52631SKirk McKusick 		else
3735cc52631SKirk McKusick 			LIST_REMOVE(bp, b_hash);
3745cc52631SKirk McKusick 	}
375460ed610SKirk McKusick 	TAILQ_REMOVE(&bufqueuehd, bp, b_list);
3765cc52631SKirk McKusick 	flush(fswritefd, bp);
377ed75b5a1SKirk McKusick 	bp->b_type = type;
3785cc52631SKirk McKusick 	LIST_INSERT_HEAD(bhdp, bp, b_hash);
3794336716bSAdrian Chadd 	getblk(bp, blkno, size);
3805cc52631SKirk McKusick 	cachereads++;
3814336716bSAdrian Chadd 	/* fall through */
3824336716bSAdrian Chadd foundit:
383460ed610SKirk McKusick 	TAILQ_INSERT_HEAD(&bufqueuehd, bp, b_list);
3845cc52631SKirk McKusick 	if (debug && bp->b_type != type) {
3855cc52631SKirk McKusick 		printf("getdatablk: buffer type changed to %s",
3865cc52631SKirk McKusick 		    BT_BUFTYPE(type));
387460ed610SKirk McKusick 		prtbuf(bp, "");
3885cc52631SKirk McKusick 	}
3895cc52631SKirk McKusick 	if (bp->b_errs == 0)
3905cc52631SKirk McKusick 		bp->b_refcnt++;
3914336716bSAdrian Chadd 	return (bp);
3924336716bSAdrian Chadd }
3934336716bSAdrian Chadd 
3944336716bSAdrian Chadd void
getblk(struct bufarea * bp,ufs2_daddr_t blk,long size)3951c85e6a3SKirk McKusick getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
3964336716bSAdrian Chadd {
3971c85e6a3SKirk McKusick 	ufs2_daddr_t dblk;
398ed75b5a1SKirk McKusick 	struct timespec start, finish;
3994336716bSAdrian Chadd 
4004336716bSAdrian Chadd 	dblk = fsbtodb(&sblock, blk);
401ed75b5a1SKirk McKusick 	if (bp->b_bno == dblk) {
402ed75b5a1SKirk McKusick 		totalreads++;
403ed75b5a1SKirk McKusick 	} else {
404ed75b5a1SKirk McKusick 		if (debug) {
405ed75b5a1SKirk McKusick 			readcnt[bp->b_type]++;
406ed75b5a1SKirk McKusick 			clock_gettime(CLOCK_REALTIME_PRECISE, &start);
407ed75b5a1SKirk McKusick 		}
408460ed610SKirk McKusick 		bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, dblk, size);
409ed75b5a1SKirk McKusick 		if (debug) {
410ed75b5a1SKirk McKusick 			clock_gettime(CLOCK_REALTIME_PRECISE, &finish);
4116040822cSAlan Somers 			timespecsub(&finish, &start, &finish);
4126040822cSAlan Somers 			timespecadd(&readtime[bp->b_type], &finish,
4136040822cSAlan Somers 			    &readtime[bp->b_type]);
414ed75b5a1SKirk McKusick 		}
4154336716bSAdrian Chadd 		bp->b_bno = dblk;
4164336716bSAdrian Chadd 		bp->b_size = size;
4174336716bSAdrian Chadd 	}
4184336716bSAdrian Chadd }
4194336716bSAdrian Chadd 
4204336716bSAdrian Chadd void
brelse(struct bufarea * bp)4215cc52631SKirk McKusick brelse(struct bufarea *bp)
4224336716bSAdrian Chadd {
4234336716bSAdrian Chadd 
4245cc52631SKirk McKusick 	if (bp->b_refcnt <= 0)
425460ed610SKirk McKusick 		prtbuf(bp, "brelse: buffer with negative reference count");
4265cc52631SKirk McKusick 	bp->b_refcnt--;
4275cc52631SKirk McKusick }
4285cc52631SKirk McKusick 
4295cc52631SKirk McKusick void
binval(struct bufarea * bp)430460ed610SKirk McKusick binval(struct bufarea *bp)
431460ed610SKirk McKusick {
432460ed610SKirk McKusick 
433460ed610SKirk McKusick 	bp->b_flags &= ~B_DIRTY;
434460ed610SKirk McKusick 	LIST_REMOVE(bp, b_hash);
435460ed610SKirk McKusick 	LIST_INSERT_HEAD(&freebufs, bp, b_hash);
436460ed610SKirk McKusick }
437460ed610SKirk McKusick 
438460ed610SKirk McKusick void
flush(int fd,struct bufarea * bp)4395cc52631SKirk McKusick flush(int fd, struct bufarea *bp)
4405cc52631SKirk McKusick {
4415cc52631SKirk McKusick 	struct inode ip;
4425cc52631SKirk McKusick 
4435cc52631SKirk McKusick 	if ((bp->b_flags & B_DIRTY) == 0)
4444336716bSAdrian Chadd 		return;
4455cc52631SKirk McKusick 	bp->b_flags &= ~B_DIRTY;
4467578c6abSKirk McKusick 	if (fswritefd < 0) {
4477578c6abSKirk McKusick 		pfatal("WRITING IN READ_ONLY MODE.\n");
4487578c6abSKirk McKusick 		return;
4497578c6abSKirk McKusick 	}
4504336716bSAdrian Chadd 	if (bp->b_errs != 0)
451599304a4SPoul-Henning Kamp 		pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
4524336716bSAdrian Chadd 		    (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
453599304a4SPoul-Henning Kamp 		    (long long)bp->b_bno);
4544336716bSAdrian Chadd 	bp->b_errs = 0;
455a6bbdf81SKirk McKusick 	/*
456a6bbdf81SKirk McKusick 	 * Write using the appropriate function.
457a6bbdf81SKirk McKusick 	 */
458a6bbdf81SKirk McKusick 	switch (bp->b_type) {
459a6bbdf81SKirk McKusick 	case BT_SUPERBLK:
4604336716bSAdrian Chadd 		if (bp != &sblk)
461af89fcf7SLi-Wen Hsu 			pfatal("BUFFER %p DOES NOT MATCH SBLK %p\n",
462af89fcf7SLi-Wen Hsu 			    bp, &sblk);
463460ed610SKirk McKusick 		/*
464460ed610SKirk McKusick 		 * Superblocks are always pre-copied so we do not need
465460ed610SKirk McKusick 		 * to check them for copy-on-write.
466460ed610SKirk McKusick 		 */
4678ebae128SKirk McKusick 		if (sbput(fd, bp->b_un.b_fs, 0) == 0)
468dffce215SKirk McKusick 			fsmodified = 1;
469a6bbdf81SKirk McKusick 		break;
470a6bbdf81SKirk McKusick 	case BT_CYLGRP:
471460ed610SKirk McKusick 		/*
472460ed610SKirk McKusick 		 * Cylinder groups are always pre-copied so we do not
473460ed610SKirk McKusick 		 * need to check them for copy-on-write.
474460ed610SKirk McKusick 		 */
4755cc52631SKirk McKusick 		if (sujrecovery)
4765cc52631SKirk McKusick 			cg_write(bp);
47785ee267aSKirk McKusick 		if (cgput(fswritefd, &sblock, bp->b_un.b_cg) == 0)
478a6bbdf81SKirk McKusick 			fsmodified = 1;
479a6bbdf81SKirk McKusick 		break;
4805cc52631SKirk McKusick 	case BT_INODES:
4815cc52631SKirk McKusick 		if (debug && sblock.fs_magic == FS_UFS2_MAGIC) {
4825cc52631SKirk McKusick 			struct ufs2_dinode *dp = bp->b_un.b_dinode2;
4835cc52631SKirk McKusick 			int i;
4845cc52631SKirk McKusick 
4852e4da012SKirk McKusick 			for (i = 0; i < bp->b_size; dp++, i += sizeof(*dp)) {
4865cc52631SKirk McKusick 				if (ffs_verify_dinode_ckhash(&sblock, dp) == 0)
4875cc52631SKirk McKusick 					continue;
4885cc52631SKirk McKusick 				pwarn("flush: INODE CHECK-HASH FAILED");
4895cc52631SKirk McKusick 				ip.i_bp = bp;
4905cc52631SKirk McKusick 				ip.i_dp = (union dinode *)dp;
4914b4cc78aSKirk McKusick 				ip.i_number = bp->b_index + (i / sizeof(*dp));
4925cc52631SKirk McKusick 				prtinode(&ip);
4935cc52631SKirk McKusick 				if (preen || reply("FIX") != 0) {
4945cc52631SKirk McKusick 					if (preen)
4955cc52631SKirk McKusick 						printf(" (FIXED)\n");
4965cc52631SKirk McKusick 					ffs_update_dinode_ckhash(&sblock, dp);
4975cc52631SKirk McKusick 					inodirty(&ip);
4985cc52631SKirk McKusick 				}
4995cc52631SKirk McKusick 			}
5005cc52631SKirk McKusick 		}
5015cc52631SKirk McKusick 		/* FALLTHROUGH */
502a6bbdf81SKirk McKusick 	default:
503460ed610SKirk McKusick 		copyonwrite(&sblock, bp, std_checkblkavail);
504a6bbdf81SKirk McKusick 		blwrite(fd, bp->b_un.b_buf, bp->b_bno, bp->b_size);
505a6bbdf81SKirk McKusick 		break;
506a6bbdf81SKirk McKusick 	}
5074336716bSAdrian Chadd }
5084336716bSAdrian Chadd 
5095cc52631SKirk McKusick /*
510460ed610SKirk McKusick  * If there are any snapshots, ensure that all the blocks that they
511460ed610SKirk McKusick  * care about have been copied, then release the snapshot inodes.
512460ed610SKirk McKusick  * These operations need to be done before we rebuild the cylinder
513460ed610SKirk McKusick  * groups so that any block allocations are properly recorded.
514460ed610SKirk McKusick  * Since all the cylinder group maps have already been copied in
515460ed610SKirk McKusick  * the snapshots, no further snapshot copies will need to be done.
516460ed610SKirk McKusick  */
517460ed610SKirk McKusick void
snapflush(ufs2_daddr_t (* checkblkavail)(ufs2_daddr_t,long))518689a9368SKirk McKusick snapflush(ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t, long))
519460ed610SKirk McKusick {
520460ed610SKirk McKusick 	struct bufarea *bp;
521460ed610SKirk McKusick 	int cnt;
522460ed610SKirk McKusick 
523460ed610SKirk McKusick 	if (snapcnt > 0) {
524460ed610SKirk McKusick 		if (debug)
525460ed610SKirk McKusick 			printf("Check for snapshot copies\n");
526460ed610SKirk McKusick 		TAILQ_FOREACH_REVERSE(bp, &bufqueuehd, bufqueue, b_list)
527460ed610SKirk McKusick 			if ((bp->b_flags & B_DIRTY) != 0)
528460ed610SKirk McKusick 				copyonwrite(&sblock, bp, checkblkavail);
529460ed610SKirk McKusick 		for (cnt = 0; cnt < snapcnt; cnt++)
530460ed610SKirk McKusick 			irelse(&snaplist[cnt]);
531460ed610SKirk McKusick 		snapcnt = 0;
532460ed610SKirk McKusick 	}
533460ed610SKirk McKusick }
534460ed610SKirk McKusick 
535460ed610SKirk McKusick /*
5365cc52631SKirk McKusick  * Journaled soft updates does not maintain cylinder group summary
5375cc52631SKirk McKusick  * information during cleanup, so this routine recalculates the summary
5385cc52631SKirk McKusick  * information and updates the superblock summary in preparation for
5395cc52631SKirk McKusick  * writing out the cylinder group.
5405cc52631SKirk McKusick  */
5415cc52631SKirk McKusick static void
cg_write(struct bufarea * bp)5425cc52631SKirk McKusick cg_write(struct bufarea *bp)
5435cc52631SKirk McKusick {
5445cc52631SKirk McKusick 	ufs1_daddr_t fragno, cgbno, maxbno;
5455cc52631SKirk McKusick 	u_int8_t *blksfree;
546460ed610SKirk McKusick 	struct csum *csp;
5475cc52631SKirk McKusick 	struct cg *cgp;
5485cc52631SKirk McKusick 	int blk;
5495cc52631SKirk McKusick 	int i;
5505cc52631SKirk McKusick 
5515cc52631SKirk McKusick 	/*
5525cc52631SKirk McKusick 	 * Fix the frag and cluster summary.
5535cc52631SKirk McKusick 	 */
5545cc52631SKirk McKusick 	cgp = bp->b_un.b_cg;
5555cc52631SKirk McKusick 	cgp->cg_cs.cs_nbfree = 0;
5565cc52631SKirk McKusick 	cgp->cg_cs.cs_nffree = 0;
5575cc52631SKirk McKusick 	bzero(&cgp->cg_frsum, sizeof(cgp->cg_frsum));
5585cc52631SKirk McKusick 	maxbno = fragstoblks(&sblock, sblock.fs_fpg);
5595cc52631SKirk McKusick 	if (sblock.fs_contigsumsize > 0) {
5605cc52631SKirk McKusick 		for (i = 1; i <= sblock.fs_contigsumsize; i++)
5615cc52631SKirk McKusick 			cg_clustersum(cgp)[i] = 0;
5625cc52631SKirk McKusick 		bzero(cg_clustersfree(cgp), howmany(maxbno, CHAR_BIT));
5635cc52631SKirk McKusick 	}
5645cc52631SKirk McKusick 	blksfree = cg_blksfree(cgp);
5655cc52631SKirk McKusick 	for (cgbno = 0; cgbno < maxbno; cgbno++) {
5665cc52631SKirk McKusick 		if (ffs_isfreeblock(&sblock, blksfree, cgbno))
5675cc52631SKirk McKusick 			continue;
5685cc52631SKirk McKusick 		if (ffs_isblock(&sblock, blksfree, cgbno)) {
5695cc52631SKirk McKusick 			ffs_clusteracct(&sblock, cgp, cgbno, 1);
5705cc52631SKirk McKusick 			cgp->cg_cs.cs_nbfree++;
5715cc52631SKirk McKusick 			continue;
5725cc52631SKirk McKusick 		}
5735cc52631SKirk McKusick 		fragno = blkstofrags(&sblock, cgbno);
5745cc52631SKirk McKusick 		blk = blkmap(&sblock, blksfree, fragno);
5755cc52631SKirk McKusick 		ffs_fragacct(&sblock, blk, cgp->cg_frsum, 1);
5765cc52631SKirk McKusick 		for (i = 0; i < sblock.fs_frag; i++)
5775cc52631SKirk McKusick 			if (isset(blksfree, fragno + i))
5785cc52631SKirk McKusick 				cgp->cg_cs.cs_nffree++;
5795cc52631SKirk McKusick 	}
5805cc52631SKirk McKusick 	/*
5815cc52631SKirk McKusick 	 * Update the superblock cg summary from our now correct values
5825cc52631SKirk McKusick 	 * before writing the block.
5835cc52631SKirk McKusick 	 */
584460ed610SKirk McKusick 	csp = &sblock.fs_cs(&sblock, cgp->cg_cgx);
585460ed610SKirk McKusick 	sblock.fs_cstotal.cs_ndir += cgp->cg_cs.cs_ndir - csp->cs_ndir;
586460ed610SKirk McKusick 	sblock.fs_cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree - csp->cs_nbfree;
587460ed610SKirk McKusick 	sblock.fs_cstotal.cs_nifree += cgp->cg_cs.cs_nifree - csp->cs_nifree;
588460ed610SKirk McKusick 	sblock.fs_cstotal.cs_nffree += cgp->cg_cs.cs_nffree - csp->cs_nffree;
5895cc52631SKirk McKusick 	sblock.fs_cs(&sblock, cgp->cg_cgx) = cgp->cg_cs;
5905cc52631SKirk McKusick }
5915cc52631SKirk McKusick 
5927578c6abSKirk McKusick void
rwerror(const char * mesg,ufs2_daddr_t blk)593599304a4SPoul-Henning Kamp rwerror(const char *mesg, ufs2_daddr_t blk)
5944336716bSAdrian Chadd {
5954336716bSAdrian Chadd 
59615fca934SKirk McKusick 	if (bkgrdcheck)
59715fca934SKirk McKusick 		exit(EEXIT);
5984336716bSAdrian Chadd 	if (preen == 0)
5994336716bSAdrian Chadd 		printf("\n");
600bf58d635SIan Dowse 	pfatal("CANNOT %s: %ld", mesg, (long)blk);
6014336716bSAdrian Chadd 	if (reply("CONTINUE") == 0)
6024336716bSAdrian Chadd 		exit(EEXIT);
6034336716bSAdrian Chadd }
6044336716bSAdrian Chadd 
6054336716bSAdrian Chadd void
ckfini(int markclean)606b70cd7eeSWarner Losh ckfini(int markclean)
6074336716bSAdrian Chadd {
6083d438ad6SDavid E. O'Brien 	struct bufarea *bp, *nbp;
60952f97104SKirk McKusick 	int ofsmodified, cnt, cg;
6104336716bSAdrian Chadd 
6117578c6abSKirk McKusick 	if (bkgrdflag) {
6127578c6abSKirk McKusick 		if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) {
6137578c6abSKirk McKusick 			cmd.value = FS_UNCLEAN;
6147578c6abSKirk McKusick 			cmd.size = markclean ? -1 : 1;
6157578c6abSKirk McKusick 			if (sysctlbyname("vfs.ffs.setflags", 0, 0,
6167578c6abSKirk McKusick 			    &cmd, sizeof cmd) == -1)
617c0bfa109SKirk McKusick 				pwarn("CANNOT SET FILE SYSTEM DIRTY FLAG\n");
6187578c6abSKirk McKusick 			if (!preen) {
6197578c6abSKirk McKusick 				printf("\n***** FILE SYSTEM MARKED %s *****\n",
6207578c6abSKirk McKusick 				    markclean ? "CLEAN" : "DIRTY");
6217578c6abSKirk McKusick 				if (!markclean)
6227578c6abSKirk McKusick 					rerun = 1;
6237578c6abSKirk McKusick 			}
6247578c6abSKirk McKusick 		} else if (!preen && !markclean) {
6257578c6abSKirk McKusick 			printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
6267578c6abSKirk McKusick 			rerun = 1;
6277578c6abSKirk McKusick 		}
628c0bfa109SKirk McKusick 		bkgrdflag = 0;
6297578c6abSKirk McKusick 	}
63020123b25SRobert Wing 	if (debug && cachelookups > 0)
6315cc52631SKirk McKusick 		printf("cache with %d buffers missed %d of %d (%d%%)\n",
6325cc52631SKirk McKusick 		    numbufs, cachereads, cachelookups,
6335cc52631SKirk McKusick 		    (int)(cachereads * 100 / cachelookups));
6344336716bSAdrian Chadd 	if (fswritefd < 0) {
6354336716bSAdrian Chadd 		(void)close(fsreadfd);
6364336716bSAdrian Chadd 		return;
6374336716bSAdrian Chadd 	}
638460ed610SKirk McKusick 
6395cc52631SKirk McKusick 	/*
6405cc52631SKirk McKusick 	 * To remain idempotent with partial truncations the buffers
6415cc52631SKirk McKusick 	 * must be flushed in this order:
6425cc52631SKirk McKusick 	 *  1) cylinder groups (bitmaps)
6435cc52631SKirk McKusick 	 *  2) indirect, directory, external attribute, and data blocks
6445cc52631SKirk McKusick 	 *  3) inode blocks
6455cc52631SKirk McKusick 	 *  4) superblock
6465cc52631SKirk McKusick 	 * This ordering preserves access to the modified pointers
6475cc52631SKirk McKusick 	 * until they are freed.
6485cc52631SKirk McKusick 	 */
6495cc52631SKirk McKusick 	/* Step 1: cylinder groups */
6505cc52631SKirk McKusick 	if (debug)
6515cc52631SKirk McKusick 		printf("Flush Cylinder groups\n");
6525cc52631SKirk McKusick 	if (cgbufs != NULL) {
6535cc52631SKirk McKusick 		for (cnt = 0; cnt < sblock.fs_ncg; cnt++) {
6545cc52631SKirk McKusick 			if (cgbufs[cnt].b_un.b_cg == NULL)
6555cc52631SKirk McKusick 				continue;
6565cc52631SKirk McKusick 			flush(fswritefd, &cgbufs[cnt]);
6575cc52631SKirk McKusick 			free(cgbufs[cnt].b_un.b_cg);
6585cc52631SKirk McKusick 		}
6595cc52631SKirk McKusick 		free(cgbufs);
660fc56fd26SKirk McKusick 		cgbufs = NULL;
6615cc52631SKirk McKusick 	}
6625cc52631SKirk McKusick 	flush(fswritefd, &cgblk);
6635cc52631SKirk McKusick 	free(cgblk.b_un.b_buf);
664fc56fd26SKirk McKusick 	cgblk.b_un.b_buf = NULL;
6655cc52631SKirk McKusick 	cnt = 0;
6665cc52631SKirk McKusick 	/* Step 2: indirect, directory, external attribute, and data blocks */
6675cc52631SKirk McKusick 	if (debug)
6685cc52631SKirk McKusick 		printf("Flush indirect, directory, external attribute, "
6695cc52631SKirk McKusick 		    "and data blocks\n");
670fc56fd26SKirk McKusick 	if (pdirbp != NULL) {
6715cc52631SKirk McKusick 		brelse(pdirbp);
672fc56fd26SKirk McKusick 		pdirbp = NULL;
673fc56fd26SKirk McKusick 	}
6745cc52631SKirk McKusick 	TAILQ_FOREACH_REVERSE_SAFE(bp, &bufqueuehd, bufqueue, b_list, nbp) {
6755cc52631SKirk McKusick 		switch (bp->b_type) {
6765cc52631SKirk McKusick 		/* These should not be in the buffer cache list */
6775cc52631SKirk McKusick 		case BT_UNKNOWN:
6785cc52631SKirk McKusick 		case BT_SUPERBLK:
6795cc52631SKirk McKusick 		case BT_CYLGRP:
6805cc52631SKirk McKusick 		default:
681460ed610SKirk McKusick 			prtbuf(bp,"ckfini: improper buffer type on cache list");
6825cc52631SKirk McKusick 			continue;
6835cc52631SKirk McKusick 		/* These are the ones to flush in this step */
6845cc52631SKirk McKusick 		case BT_LEVEL1:
6855cc52631SKirk McKusick 		case BT_LEVEL2:
6865cc52631SKirk McKusick 		case BT_LEVEL3:
6875cc52631SKirk McKusick 		case BT_EXTATTR:
6885cc52631SKirk McKusick 		case BT_DIRDATA:
6895cc52631SKirk McKusick 		case BT_DATA:
6905cc52631SKirk McKusick 			break;
6915cc52631SKirk McKusick 		/* These are the ones to flush in the next step */
6925cc52631SKirk McKusick 		case BT_INODES:
6935cc52631SKirk McKusick 			continue;
6945cc52631SKirk McKusick 		}
695460ed610SKirk McKusick 		if (debug && bp->b_refcnt != 0)
696460ed610SKirk McKusick 			prtbuf(bp, "ckfini: clearing in-use buffer");
6975cc52631SKirk McKusick 		TAILQ_REMOVE(&bufqueuehd, bp, b_list);
698460ed610SKirk McKusick 		LIST_REMOVE(bp, b_hash);
6995cc52631SKirk McKusick 		cnt++;
7005cc52631SKirk McKusick 		flush(fswritefd, bp);
7015cc52631SKirk McKusick 		free(bp->b_un.b_buf);
7025cc52631SKirk McKusick 		free((char *)bp);
7035cc52631SKirk McKusick 	}
7045cc52631SKirk McKusick 	/* Step 3: inode blocks */
7055cc52631SKirk McKusick 	if (debug)
7065cc52631SKirk McKusick 		printf("Flush inode blocks\n");
707fc56fd26SKirk McKusick 	if (icachebp != NULL) {
7085cc52631SKirk McKusick 		brelse(icachebp);
709fc56fd26SKirk McKusick 		icachebp = NULL;
710fc56fd26SKirk McKusick 	}
7115cc52631SKirk McKusick 	TAILQ_FOREACH_REVERSE_SAFE(bp, &bufqueuehd, bufqueue, b_list, nbp) {
712460ed610SKirk McKusick 		if (debug && bp->b_refcnt != 0)
713460ed610SKirk McKusick 			prtbuf(bp, "ckfini: clearing in-use buffer");
7145cc52631SKirk McKusick 		TAILQ_REMOVE(&bufqueuehd, bp, b_list);
715460ed610SKirk McKusick 		LIST_REMOVE(bp, b_hash);
7165cc52631SKirk McKusick 		cnt++;
7175cc52631SKirk McKusick 		flush(fswritefd, bp);
7185cc52631SKirk McKusick 		free(bp->b_un.b_buf);
7195cc52631SKirk McKusick 		free((char *)bp);
7205cc52631SKirk McKusick 	}
7215cc52631SKirk McKusick 	if (numbufs != cnt)
7225cc52631SKirk McKusick 		errx(EEXIT, "panic: lost %d buffers", numbufs - cnt);
7235cc52631SKirk McKusick 	/* Step 4: superblock */
7245cc52631SKirk McKusick 	if (debug)
7255cc52631SKirk McKusick 		printf("Flush the superblock\n");
7264336716bSAdrian Chadd 	flush(fswritefd, &sblk);
727be639cc8SKirk McKusick 	if (havesb && cursnapshot == 0 &&
728be639cc8SKirk McKusick 	    sblk.b_bno != sblock.fs_sblockloc / dev_bsize) {
729be639cc8SKirk McKusick 		if (preen || reply("UPDATE STANDARD SUPERBLOCK")) {
730be639cc8SKirk McKusick 			/* Change write destination to standard superblock */
731dffce215SKirk McKusick 			sblock.fs_sblockactualloc = sblock.fs_sblockloc;
732ada981b2SKirk McKusick 			sblk.b_bno = sblock.fs_sblockloc / dev_bsize;
7334336716bSAdrian Chadd 			sbdirty();
7344336716bSAdrian Chadd 			flush(fswritefd, &sblk);
735be639cc8SKirk McKusick 		} else {
736be639cc8SKirk McKusick 			markclean = 0;
737be639cc8SKirk McKusick 		}
7384336716bSAdrian Chadd 	}
7397578c6abSKirk McKusick 	if (cursnapshot == 0 && sblock.fs_clean != markclean) {
74068aff084SKirk McKusick 		if ((sblock.fs_clean = markclean) != 0) {
74138375c40SKirk McKusick 			sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK);
74268aff084SKirk McKusick 			sblock.fs_pendingblocks = 0;
74368aff084SKirk McKusick 			sblock.fs_pendinginodes = 0;
74468aff084SKirk McKusick 		}
7454336716bSAdrian Chadd 		sbdirty();
7464336716bSAdrian Chadd 		ofsmodified = fsmodified;
7474336716bSAdrian Chadd 		flush(fswritefd, &sblk);
7484336716bSAdrian Chadd 		fsmodified = ofsmodified;
7494336716bSAdrian Chadd 		if (!preen) {
7504336716bSAdrian Chadd 			printf("\n***** FILE SYSTEM MARKED %s *****\n",
7514336716bSAdrian Chadd 			    markclean ? "CLEAN" : "DIRTY");
7524336716bSAdrian Chadd 			if (!markclean)
7534336716bSAdrian Chadd 				rerun = 1;
7544336716bSAdrian Chadd 		}
755910b491eSKirk McKusick 	} else if (!preen) {
756910b491eSKirk McKusick 		if (markclean) {
757910b491eSKirk McKusick 			printf("\n***** FILE SYSTEM IS CLEAN *****\n");
758910b491eSKirk McKusick 		} else {
7594336716bSAdrian Chadd 			printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
7604336716bSAdrian Chadd 			rerun = 1;
7614336716bSAdrian Chadd 		}
762910b491eSKirk McKusick 	}
763fc56fd26SKirk McKusick 	/*
764fc56fd26SKirk McKusick 	 * Free allocated tracking structures.
765fc56fd26SKirk McKusick 	 */
766fc56fd26SKirk McKusick 	if (blockmap != NULL)
767fc56fd26SKirk McKusick 		free(blockmap);
768fc56fd26SKirk McKusick 	blockmap = NULL;
769fc56fd26SKirk McKusick 	if (inostathead != NULL) {
770fc56fd26SKirk McKusick 		for (cg = 0; cg < sblock.fs_ncg; cg++)
771fc56fd26SKirk McKusick 			if (inostathead[cg].il_stat != NULL)
772fc56fd26SKirk McKusick 				free((char *)inostathead[cg].il_stat);
773fc56fd26SKirk McKusick 		free(inostathead);
774fc56fd26SKirk McKusick 	}
775fc56fd26SKirk McKusick 	inostathead = NULL;
77652f97104SKirk McKusick 	inocleanup();
7775cc52631SKirk McKusick 	finalIOstats();
7784336716bSAdrian Chadd 	(void)close(fsreadfd);
7794336716bSAdrian Chadd 	(void)close(fswritefd);
7804336716bSAdrian Chadd }
7814336716bSAdrian Chadd 
782ed75b5a1SKirk McKusick /*
783ed75b5a1SKirk McKusick  * Print out I/O statistics.
784ed75b5a1SKirk McKusick  */
785ed75b5a1SKirk McKusick void
IOstats(char * what)786ed75b5a1SKirk McKusick IOstats(char *what)
787ed75b5a1SKirk McKusick {
788ed75b5a1SKirk McKusick 	int i;
789ed75b5a1SKirk McKusick 
790ed75b5a1SKirk McKusick 	if (debug == 0)
791ed75b5a1SKirk McKusick 		return;
792ed75b5a1SKirk McKusick 	if (diskreads == 0) {
793ed75b5a1SKirk McKusick 		printf("%s: no I/O\n\n", what);
794ed75b5a1SKirk McKusick 		return;
795ed75b5a1SKirk McKusick 	}
796ed75b5a1SKirk McKusick 	if (startpass.tv_sec == 0)
797ed75b5a1SKirk McKusick 		startpass = startprog;
798ed75b5a1SKirk McKusick 	printf("%s: I/O statistics\n", what);
799ed75b5a1SKirk McKusick 	printIOstats();
800ed75b5a1SKirk McKusick 	totaldiskreads += diskreads;
801ed75b5a1SKirk McKusick 	diskreads = 0;
802ed75b5a1SKirk McKusick 	for (i = 0; i < BT_NUMBUFTYPES; i++) {
8036040822cSAlan Somers 		timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
804ed75b5a1SKirk McKusick 		totalreadcnt[i] += readcnt[i];
805ed75b5a1SKirk McKusick 		readtime[i].tv_sec = readtime[i].tv_nsec = 0;
806ed75b5a1SKirk McKusick 		readcnt[i] = 0;
807ed75b5a1SKirk McKusick 	}
808ed75b5a1SKirk McKusick 	clock_gettime(CLOCK_REALTIME_PRECISE, &startpass);
809ed75b5a1SKirk McKusick }
810ed75b5a1SKirk McKusick 
811ed75b5a1SKirk McKusick void
finalIOstats(void)812ed75b5a1SKirk McKusick finalIOstats(void)
813ed75b5a1SKirk McKusick {
814ed75b5a1SKirk McKusick 	int i;
815ed75b5a1SKirk McKusick 
816ed75b5a1SKirk McKusick 	if (debug == 0)
817ed75b5a1SKirk McKusick 		return;
818ed75b5a1SKirk McKusick 	printf("Final I/O statistics\n");
819ed75b5a1SKirk McKusick 	totaldiskreads += diskreads;
820ed75b5a1SKirk McKusick 	diskreads = totaldiskreads;
821ed75b5a1SKirk McKusick 	startpass = startprog;
822ed75b5a1SKirk McKusick 	for (i = 0; i < BT_NUMBUFTYPES; i++) {
8236040822cSAlan Somers 		timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
824ed75b5a1SKirk McKusick 		totalreadcnt[i] += readcnt[i];
825ed75b5a1SKirk McKusick 		readtime[i] = totalreadtime[i];
826ed75b5a1SKirk McKusick 		readcnt[i] = totalreadcnt[i];
827ed75b5a1SKirk McKusick 	}
828ed75b5a1SKirk McKusick 	printIOstats();
829ed75b5a1SKirk McKusick }
830ed75b5a1SKirk McKusick 
printIOstats(void)831ed75b5a1SKirk McKusick static void printIOstats(void)
832ed75b5a1SKirk McKusick {
833ed75b5a1SKirk McKusick 	long long msec, totalmsec;
834ed75b5a1SKirk McKusick 	int i;
835ed75b5a1SKirk McKusick 
836ed75b5a1SKirk McKusick 	clock_gettime(CLOCK_REALTIME_PRECISE, &finishpass);
8376040822cSAlan Somers 	timespecsub(&finishpass, &startpass, &finishpass);
838061ea59dSKirk McKusick 	printf("Running time: %jd.%03ld sec\n",
8394b3bbe04SSean Bruno 		(intmax_t)finishpass.tv_sec, finishpass.tv_nsec / 1000000);
840ed75b5a1SKirk McKusick 	printf("buffer reads by type:\n");
841ed75b5a1SKirk McKusick 	for (totalmsec = 0, i = 0; i < BT_NUMBUFTYPES; i++)
842ed75b5a1SKirk McKusick 		totalmsec += readtime[i].tv_sec * 1000 +
843ed75b5a1SKirk McKusick 		    readtime[i].tv_nsec / 1000000;
844ed75b5a1SKirk McKusick 	if (totalmsec == 0)
845ed75b5a1SKirk McKusick 		totalmsec = 1;
846ed75b5a1SKirk McKusick 	for (i = 0; i < BT_NUMBUFTYPES; i++) {
847ed75b5a1SKirk McKusick 		if (readcnt[i] == 0)
848ed75b5a1SKirk McKusick 			continue;
849061ea59dSKirk McKusick 		msec =
850061ea59dSKirk McKusick 		    readtime[i].tv_sec * 1000 + readtime[i].tv_nsec / 1000000;
8514b3bbe04SSean Bruno 		printf("%21s:%8ld %2ld.%ld%% %4jd.%03ld sec %2lld.%lld%%\n",
852ed75b5a1SKirk McKusick 		    buftype[i], readcnt[i], readcnt[i] * 100 / diskreads,
85381fbded2SKirk McKusick 		    (readcnt[i] * 1000 / diskreads) % 10,
8544b3bbe04SSean Bruno 		    (intmax_t)readtime[i].tv_sec, readtime[i].tv_nsec / 1000000,
855ed75b5a1SKirk McKusick 		    msec * 100 / totalmsec, (msec * 1000 / totalmsec) % 10);
856ed75b5a1SKirk McKusick 	}
857ed75b5a1SKirk McKusick 	printf("\n");
858ed75b5a1SKirk McKusick }
859ed75b5a1SKirk McKusick 
8604336716bSAdrian Chadd int
blread(int fd,char * buf,ufs2_daddr_t blk,long size)861aef8d244SPawel Jakub Dawidek blread(int fd, char *buf, ufs2_daddr_t blk, long size)
8624336716bSAdrian Chadd {
8634336716bSAdrian Chadd 	char *cp;
8644336716bSAdrian Chadd 	int i, errs;
8654336716bSAdrian Chadd 	off_t offset;
8664336716bSAdrian Chadd 
8674336716bSAdrian Chadd 	offset = blk;
8684336716bSAdrian Chadd 	offset *= dev_bsize;
8699d580d7cSIan Dowse 	if (bkgrdflag)
8709d580d7cSIan Dowse 		slowio_start();
871ed75b5a1SKirk McKusick 	totalreads++;
872ed75b5a1SKirk McKusick 	diskreads++;
87304e5c6f1SEdward Tomasz Napierala 	if (pread(fd, buf, (int)size, offset) == size) {
8749d580d7cSIan Dowse 		if (bkgrdflag)
8759d580d7cSIan Dowse 			slowio_end();
8764336716bSAdrian Chadd 		return (0);
8779d580d7cSIan Dowse 	}
878ce779f37SScott Long 
879ce779f37SScott Long 	/*
880ce779f37SScott Long 	 * This is handled specially here instead of in rwerror because
881ce779f37SScott Long 	 * rwerror is used for all sorts of errors, not just true read/write
882ce779f37SScott Long 	 * errors.  It should be refactored and fixed.
883ce779f37SScott Long 	 */
884ce779f37SScott Long 	if (surrender) {
885ce779f37SScott Long 		pfatal("CANNOT READ_BLK: %ld", (long)blk);
886ce779f37SScott Long 		errx(EEXIT, "ABORTING DUE TO READ ERRORS");
887ce779f37SScott Long 	} else
8887578c6abSKirk McKusick 		rwerror("READ BLK", blk);
889ce779f37SScott Long 
8904336716bSAdrian Chadd 	errs = 0;
8914336716bSAdrian Chadd 	memset(buf, 0, (size_t)size);
8924336716bSAdrian Chadd 	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
8934336716bSAdrian Chadd 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
89404e5c6f1SEdward Tomasz Napierala 		if (pread(fd, cp, (int)secsize, offset + i) != secsize) {
8954336716bSAdrian Chadd 			if (secsize != dev_bsize && dev_bsize != 1)
89684fc0d7eSMaxime Henrion 				printf(" %jd (%jd),",
89784fc0d7eSMaxime Henrion 				    (intmax_t)(blk * dev_bsize + i) / secsize,
89884fc0d7eSMaxime Henrion 				    (intmax_t)blk + i / dev_bsize);
8994336716bSAdrian Chadd 			else
90084fc0d7eSMaxime Henrion 				printf(" %jd,", (intmax_t)blk + i / dev_bsize);
9014336716bSAdrian Chadd 			errs++;
9024336716bSAdrian Chadd 		}
9034336716bSAdrian Chadd 	}
9044336716bSAdrian Chadd 	printf("\n");
9054336716bSAdrian Chadd 	if (errs)
9064336716bSAdrian Chadd 		resolved = 0;
9074336716bSAdrian Chadd 	return (errs);
9084336716bSAdrian Chadd }
9094336716bSAdrian Chadd 
9104336716bSAdrian Chadd void
blwrite(int fd,char * buf,ufs2_daddr_t blk,ssize_t size)9114a835375SDavid E. O'Brien blwrite(int fd, char *buf, ufs2_daddr_t blk, ssize_t size)
9124336716bSAdrian Chadd {
9134336716bSAdrian Chadd 	int i;
9144336716bSAdrian Chadd 	char *cp;
9154336716bSAdrian Chadd 	off_t offset;
9164336716bSAdrian Chadd 
9174336716bSAdrian Chadd 	if (fd < 0)
9184336716bSAdrian Chadd 		return;
9194336716bSAdrian Chadd 	offset = blk;
9204336716bSAdrian Chadd 	offset *= dev_bsize;
92104e5c6f1SEdward Tomasz Napierala 	if (pwrite(fd, buf, size, offset) == size) {
9224336716bSAdrian Chadd 		fsmodified = 1;
9234336716bSAdrian Chadd 		return;
9244336716bSAdrian Chadd 	}
9254336716bSAdrian Chadd 	resolved = 0;
9267578c6abSKirk McKusick 	rwerror("WRITE BLK", blk);
9274336716bSAdrian Chadd 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
9284336716bSAdrian Chadd 	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
92904e5c6f1SEdward Tomasz Napierala 		if (pwrite(fd, cp, dev_bsize, offset + i) != dev_bsize)
93084fc0d7eSMaxime Henrion 			printf(" %jd,", (intmax_t)blk + i / dev_bsize);
9314336716bSAdrian Chadd 	printf("\n");
9324336716bSAdrian Chadd 	return;
9334336716bSAdrian Chadd }
9344336716bSAdrian Chadd 
9358d3dfc26SDag-Erling Smørgrav void
blerase(int fd,ufs2_daddr_t blk,long size)9368d3dfc26SDag-Erling Smørgrav blerase(int fd, ufs2_daddr_t blk, long size)
9378d3dfc26SDag-Erling Smørgrav {
9388d3dfc26SDag-Erling Smørgrav 	off_t ioarg[2];
9398d3dfc26SDag-Erling Smørgrav 
9408d3dfc26SDag-Erling Smørgrav 	if (fd < 0)
9418d3dfc26SDag-Erling Smørgrav 		return;
9428d3dfc26SDag-Erling Smørgrav 	ioarg[0] = blk * dev_bsize;
9438d3dfc26SDag-Erling Smørgrav 	ioarg[1] = size;
9448d3dfc26SDag-Erling Smørgrav 	ioctl(fd, DIOCGDELETE, ioarg);
9458d3dfc26SDag-Erling Smørgrav 	/* we don't really care if we succeed or not */
9468d3dfc26SDag-Erling Smørgrav 	return;
9478d3dfc26SDag-Erling Smørgrav }
9488d3dfc26SDag-Erling Smørgrav 
9498ce80d4bSDag-Erling Smørgrav /*
9508ce80d4bSDag-Erling Smørgrav  * Fill a contiguous region with all-zeroes.  Note ZEROBUFSIZE is by
9518ce80d4bSDag-Erling Smørgrav  * definition a multiple of dev_bsize.
9528ce80d4bSDag-Erling Smørgrav  */
9532b5373deSDag-Erling Smørgrav void
blzero(int fd,ufs2_daddr_t blk,long size)9542b5373deSDag-Erling Smørgrav blzero(int fd, ufs2_daddr_t blk, long size)
9552b5373deSDag-Erling Smørgrav {
9562b5373deSDag-Erling Smørgrav 	static char *zero;
9572b5373deSDag-Erling Smørgrav 	off_t offset, len;
9582b5373deSDag-Erling Smørgrav 
9592b5373deSDag-Erling Smørgrav 	if (fd < 0)
9602b5373deSDag-Erling Smørgrav 		return;
9612b5373deSDag-Erling Smørgrav 	if (zero == NULL) {
962*772430ddSKirk McKusick 		zero = Balloc(ZEROBUFSIZE);
9632b5373deSDag-Erling Smørgrav 		if (zero == NULL)
9642b5373deSDag-Erling Smørgrav 			errx(EEXIT, "cannot allocate buffer pool");
9652b5373deSDag-Erling Smørgrav 	}
9662b5373deSDag-Erling Smørgrav 	offset = blk * dev_bsize;
9672b5373deSDag-Erling Smørgrav 	if (lseek(fd, offset, 0) < 0)
9682b5373deSDag-Erling Smørgrav 		rwerror("SEEK BLK", blk);
9692b5373deSDag-Erling Smørgrav 	while (size > 0) {
9701120faabSMarcelo Araujo 		len = MIN(ZEROBUFSIZE, size);
9712b5373deSDag-Erling Smørgrav 		if (write(fd, zero, len) != len)
9722b5373deSDag-Erling Smørgrav 			rwerror("WRITE BLK", blk);
9732b5373deSDag-Erling Smørgrav 		blk += len / dev_bsize;
9742b5373deSDag-Erling Smørgrav 		size -= len;
9752b5373deSDag-Erling Smørgrav 	}
9762b5373deSDag-Erling Smørgrav }
9772b5373deSDag-Erling Smørgrav 
9784336716bSAdrian Chadd /*
979910b491eSKirk McKusick  * Verify cylinder group's magic number and other parameters.  If the
980910b491eSKirk McKusick  * test fails, offer an option to rebuild the whole cylinder group.
981da86e7a2SKirk McKusick  *
98240647558SChuck Silvers  * Return 1 if the cylinder group is good or return 0 if it is bad.
98314320f1eSXin LI  */
984495b1baaSKirk McKusick #undef CHK
985495b1baaSKirk McKusick #define CHK(lhs, op, rhs, fmt)						\
986495b1baaSKirk McKusick 	if (lhs op rhs) {						\
98727c6009eSKirk McKusick 		pwarn("UFS%d cylinder group %d failed: "		\
98827c6009eSKirk McKusick 		    "%s (" #fmt ") %s %s (" #fmt ")\n",			\
98927c6009eSKirk McKusick 		    sblock.fs_magic == FS_UFS1_MAGIC ? 1 : 2, cg,	\
99027c6009eSKirk McKusick 		    #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs);	\
991495b1baaSKirk McKusick 		error = 1;						\
992495b1baaSKirk McKusick 	}
993910b491eSKirk McKusick int
check_cgmagic(int cg,struct bufarea * cgbp)99440647558SChuck Silvers check_cgmagic(int cg, struct bufarea *cgbp)
99514320f1eSXin LI {
99681fbded2SKirk McKusick 	struct cg *cgp = cgbp->b_un.b_cg;
9975cc52631SKirk McKusick 	uint32_t cghash, calchash;
998689724cbSKirk McKusick 	static int prevfailcg = -1;
99952f97104SKirk McKusick 	long start;
1000495b1baaSKirk McKusick 	int error;
100114320f1eSXin LI 
1002910b491eSKirk McKusick 	/*
1003910b491eSKirk McKusick 	 * Extended cylinder group checks.
1004910b491eSKirk McKusick 	 */
10055cc52631SKirk McKusick 	calchash = cgp->cg_ckhash;
10066385cabdSKirk McKusick 	if ((sblock.fs_metackhash & CK_CYLGRP) != 0 &&
10076385cabdSKirk McKusick 	    (ckhashadd & CK_CYLGRP) == 0) {
10085cc52631SKirk McKusick 		cghash = cgp->cg_ckhash;
10095cc52631SKirk McKusick 		cgp->cg_ckhash = 0;
10105cc52631SKirk McKusick 		calchash = calculate_crc32c(~0L, (void *)cgp, sblock.fs_cgsize);
10115cc52631SKirk McKusick 		cgp->cg_ckhash = cghash;
10125cc52631SKirk McKusick 	}
1013495b1baaSKirk McKusick 	error = 0;
1014495b1baaSKirk McKusick 	CHK(cgp->cg_ckhash, !=, calchash, "%jd");
1015495b1baaSKirk McKusick 	CHK(cg_chkmagic(cgp), ==, 0, "%jd");
1016495b1baaSKirk McKusick 	CHK(cgp->cg_cgx, !=, cg, "%jd");
1017495b1baaSKirk McKusick 	CHK(cgp->cg_ndblk, >, sblock.fs_fpg, "%jd");
1018495b1baaSKirk McKusick 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
1019495b1baaSKirk McKusick 		CHK(cgp->cg_old_niblk, !=, sblock.fs_ipg, "%jd");
1020495b1baaSKirk McKusick 	} else if (sblock.fs_magic == FS_UFS2_MAGIC) {
1021495b1baaSKirk McKusick 		CHK(cgp->cg_niblk, !=, sblock.fs_ipg, "%jd");
1022495b1baaSKirk McKusick 		CHK(cgp->cg_initediblk, >, sblock.fs_ipg, "%jd");
1023910b491eSKirk McKusick 	}
102452f97104SKirk McKusick 	if (cgbase(&sblock, cg) + sblock.fs_fpg < sblock.fs_size) {
102552f97104SKirk McKusick 		CHK(cgp->cg_ndblk, !=, sblock.fs_fpg, "%jd");
102652f97104SKirk McKusick 	} else {
102752f97104SKirk McKusick 		CHK(cgp->cg_ndblk, !=, sblock.fs_size - cgbase(&sblock, cg),
102852f97104SKirk McKusick 		    "%jd");
102952f97104SKirk McKusick 	}
10300a6e34e9SKirk McKusick 	start = sizeof(*cgp);
103152f97104SKirk McKusick 	if (sblock.fs_magic == FS_UFS2_MAGIC) {
103252f97104SKirk McKusick 		CHK(cgp->cg_iusedoff, !=, start, "%jd");
103352f97104SKirk McKusick 	} else if (sblock.fs_magic == FS_UFS1_MAGIC) {
103452f97104SKirk McKusick 		CHK(cgp->cg_niblk, !=, 0, "%jd");
103552f97104SKirk McKusick 		CHK(cgp->cg_initediblk, !=, 0, "%jd");
103652f97104SKirk McKusick 		CHK(cgp->cg_old_niblk, !=, sblock.fs_ipg, "%jd");
103752f97104SKirk McKusick 		CHK(cgp->cg_old_btotoff, !=, start, "%jd");
103852f97104SKirk McKusick 		CHK(cgp->cg_old_boff, !=, cgp->cg_old_btotoff +
103952f97104SKirk McKusick 		    sblock.fs_old_cpg * sizeof(int32_t), "%jd");
104052f97104SKirk McKusick 		CHK(cgp->cg_iusedoff, !=, cgp->cg_old_boff +
104152f97104SKirk McKusick 		    sblock.fs_old_cpg * sizeof(u_int16_t), "%jd");
104252f97104SKirk McKusick 	}
104352f97104SKirk McKusick 	CHK(cgp->cg_freeoff, !=,
104452f97104SKirk McKusick 	    cgp->cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT), "%jd");
104552f97104SKirk McKusick 	if (sblock.fs_contigsumsize == 0) {
104652f97104SKirk McKusick 		CHK(cgp->cg_nextfreeoff, !=,
104752f97104SKirk McKusick 		    cgp->cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT), "%jd");
104852f97104SKirk McKusick 	} else {
104952f97104SKirk McKusick 		CHK(cgp->cg_nclusterblks, !=, cgp->cg_ndblk / sblock.fs_frag,
105052f97104SKirk McKusick 		    "%jd");
105152f97104SKirk McKusick 		CHK(cgp->cg_clustersumoff, !=,
105252f97104SKirk McKusick 		    roundup(cgp->cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT),
105352f97104SKirk McKusick 		    sizeof(u_int32_t)) - sizeof(u_int32_t), "%jd");
105452f97104SKirk McKusick 		CHK(cgp->cg_clusteroff, !=, cgp->cg_clustersumoff +
105552f97104SKirk McKusick 		    (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t), "%jd");
105652f97104SKirk McKusick 		CHK(cgp->cg_nextfreeoff, !=, cgp->cg_clusteroff +
105752f97104SKirk McKusick 		    howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT),
105852f97104SKirk McKusick 		    "%jd");
105952f97104SKirk McKusick 	}
1060495b1baaSKirk McKusick 	if (error == 0)
1061495b1baaSKirk McKusick 		return (1);
1062689724cbSKirk McKusick 	if (prevfailcg == cg)
10635cc52631SKirk McKusick 		return (0);
1064689724cbSKirk McKusick 	prevfailcg = cg;
1065689724cbSKirk McKusick 	pfatal("CYLINDER GROUP %d: INTEGRITY CHECK FAILED", cg);
1066689724cbSKirk McKusick 	printf("\n");
1067689724cbSKirk McKusick 	return (0);
1068689724cbSKirk McKusick }
106940647558SChuck Silvers 
107040647558SChuck Silvers void
rebuild_cg(int cg,struct bufarea * cgbp)107140647558SChuck Silvers rebuild_cg(int cg, struct bufarea *cgbp)
107240647558SChuck Silvers {
107340647558SChuck Silvers 	struct cg *cgp = cgbp->b_un.b_cg;
107440647558SChuck Silvers 	long start;
107540647558SChuck Silvers 
1076910b491eSKirk McKusick 	/*
1077910b491eSKirk McKusick 	 * Zero out the cylinder group and then initialize critical fields.
1078910b491eSKirk McKusick 	 * Bit maps and summaries will be recalculated by later passes.
1079910b491eSKirk McKusick 	 */
108014320f1eSXin LI 	memset(cgp, 0, (size_t)sblock.fs_cgsize);
1081910b491eSKirk McKusick 	cgp->cg_magic = CG_MAGIC;
108214320f1eSXin LI 	cgp->cg_cgx = cg;
108314320f1eSXin LI 	cgp->cg_niblk = sblock.fs_ipg;
10841120faabSMarcelo Araujo 	cgp->cg_initediblk = MIN(sblock.fs_ipg, 2 * INOPB(&sblock));
1085910b491eSKirk McKusick 	if (cgbase(&sblock, cg) + sblock.fs_fpg < sblock.fs_size)
1086910b491eSKirk McKusick 		cgp->cg_ndblk = sblock.fs_fpg;
1087910b491eSKirk McKusick 	else
108814320f1eSXin LI 		cgp->cg_ndblk = sblock.fs_size - cgbase(&sblock, cg);
10890a6e34e9SKirk McKusick 	start = sizeof(*cgp);
109052f97104SKirk McKusick 	if (sblock.fs_magic == FS_UFS2_MAGIC) {
109152f97104SKirk McKusick 		cgp->cg_iusedoff = start;
109252f97104SKirk McKusick 	} else if (sblock.fs_magic == FS_UFS1_MAGIC) {
1093910b491eSKirk McKusick 		cgp->cg_niblk = 0;
1094910b491eSKirk McKusick 		cgp->cg_initediblk = 0;
1095910b491eSKirk McKusick 		cgp->cg_old_ncyl = sblock.fs_old_cpg;
1096910b491eSKirk McKusick 		cgp->cg_old_niblk = sblock.fs_ipg;
109752f97104SKirk McKusick 		cgp->cg_old_btotoff = start;
1098910b491eSKirk McKusick 		cgp->cg_old_boff = cgp->cg_old_btotoff +
1099910b491eSKirk McKusick 		    sblock.fs_old_cpg * sizeof(int32_t);
1100910b491eSKirk McKusick 		cgp->cg_iusedoff = cgp->cg_old_boff +
1101910b491eSKirk McKusick 		    sblock.fs_old_cpg * sizeof(u_int16_t);
1102910b491eSKirk McKusick 	}
1103910b491eSKirk McKusick 	cgp->cg_freeoff = cgp->cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
1104910b491eSKirk McKusick 	cgp->cg_nextfreeoff = cgp->cg_freeoff + howmany(sblock.fs_fpg,CHAR_BIT);
1105910b491eSKirk McKusick 	if (sblock.fs_contigsumsize > 0) {
1106910b491eSKirk McKusick 		cgp->cg_nclusterblks = cgp->cg_ndblk / sblock.fs_frag;
1107910b491eSKirk McKusick 		cgp->cg_clustersumoff =
1108910b491eSKirk McKusick 		    roundup(cgp->cg_nextfreeoff, sizeof(u_int32_t));
1109910b491eSKirk McKusick 		cgp->cg_clustersumoff -= sizeof(u_int32_t);
1110910b491eSKirk McKusick 		cgp->cg_clusteroff = cgp->cg_clustersumoff +
1111910b491eSKirk McKusick 		    (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
1112910b491eSKirk McKusick 		cgp->cg_nextfreeoff = cgp->cg_clusteroff +
1113910b491eSKirk McKusick 		    howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
1114910b491eSKirk McKusick 	}
1115495b1baaSKirk McKusick 	cgp->cg_ckhash = calculate_crc32c(~0L, (void *)cgp, sblock.fs_cgsize);
11168ebae128SKirk McKusick 	cgdirty(cgbp);
111714320f1eSXin LI }
111814320f1eSXin LI 
111914320f1eSXin LI /*
11204336716bSAdrian Chadd  * allocate a data block with the specified number of fragments
11214336716bSAdrian Chadd  */
11221c85e6a3SKirk McKusick ufs2_daddr_t
allocblk(long startcg,long frags,ufs2_daddr_t (* checkblkavail)(ufs2_daddr_t blkno,long frags))1123460ed610SKirk McKusick allocblk(long startcg, long frags,
1124460ed610SKirk McKusick     ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags))
11254336716bSAdrian Chadd {
1126460ed610SKirk McKusick 	ufs2_daddr_t blkno, newblk;
11274336716bSAdrian Chadd 
1128460ed610SKirk McKusick 	if (sujrecovery && checkblkavail == std_checkblkavail) {
1129460ed610SKirk McKusick 		pfatal("allocblk: std_checkblkavail used for SUJ recovery\n");
1130460ed610SKirk McKusick 		return (0);
1131460ed610SKirk McKusick 	}
11324336716bSAdrian Chadd 	if (frags <= 0 || frags > sblock.fs_frag)
11334336716bSAdrian Chadd 		return (0);
113452f97104SKirk McKusick 	for (blkno = MAX(cgdata(&sblock, startcg), 0);
1135460ed610SKirk McKusick 	     blkno < maxfsblock - sblock.fs_frag;
1136460ed610SKirk McKusick 	     blkno += sblock.fs_frag) {
1137460ed610SKirk McKusick 		if ((newblk = (*checkblkavail)(blkno, frags)) == 0)
1138460ed610SKirk McKusick 			continue;
1139460ed610SKirk McKusick 		if (newblk > 0)
1140460ed610SKirk McKusick 			return (newblk);
1141460ed610SKirk McKusick 		if (newblk < 0)
1142460ed610SKirk McKusick 			blkno = -newblk;
1143460ed610SKirk McKusick 	}
114452f97104SKirk McKusick 	for (blkno = MAX(cgdata(&sblock, 0), 0);
1145460ed610SKirk McKusick 	     blkno < cgbase(&sblock, startcg) - sblock.fs_frag;
1146460ed610SKirk McKusick 	     blkno += sblock.fs_frag) {
1147460ed610SKirk McKusick 		if ((newblk = (*checkblkavail)(blkno, frags)) == 0)
1148460ed610SKirk McKusick 			continue;
1149460ed610SKirk McKusick 		if (newblk > 0)
1150460ed610SKirk McKusick 			return (newblk);
1151460ed610SKirk McKusick 		if (newblk < 0)
1152460ed610SKirk McKusick 			blkno = -newblk;
1153460ed610SKirk McKusick 	}
1154460ed610SKirk McKusick 	return (0);
1155460ed610SKirk McKusick }
1156460ed610SKirk McKusick 
1157460ed610SKirk McKusick ufs2_daddr_t
std_checkblkavail(ufs2_daddr_t blkno,long frags)1158e5d0d1c5SKirk McKusick std_checkblkavail(ufs2_daddr_t blkno, long frags)
1159460ed610SKirk McKusick {
1160460ed610SKirk McKusick 	struct bufarea *cgbp;
1161460ed610SKirk McKusick 	struct cg *cgp;
1162460ed610SKirk McKusick 	ufs2_daddr_t j, k, baseblk;
1163460ed610SKirk McKusick 	long cg;
1164460ed610SKirk McKusick 
116552f97104SKirk McKusick 	if ((u_int64_t)blkno > sblock.fs_size)
116652f97104SKirk McKusick 		return (0);
11674336716bSAdrian Chadd 	for (j = 0; j <= sblock.fs_frag - frags; j++) {
1168460ed610SKirk McKusick 		if (testbmap(blkno + j))
11694336716bSAdrian Chadd 			continue;
11704336716bSAdrian Chadd 		for (k = 1; k < frags; k++)
1171460ed610SKirk McKusick 			if (testbmap(blkno + j + k))
11724336716bSAdrian Chadd 				break;
11734336716bSAdrian Chadd 		if (k < frags) {
11744336716bSAdrian Chadd 			j += k;
11754336716bSAdrian Chadd 			continue;
11764336716bSAdrian Chadd 		}
1177460ed610SKirk McKusick 		cg = dtog(&sblock, blkno + j);
1178957fc241SKirk McKusick 		cgbp = cglookup(cg);
117981fbded2SKirk McKusick 		cgp = cgbp->b_un.b_cg;
118040647558SChuck Silvers 		if (!check_cgmagic(cg, cgbp))
1181460ed610SKirk McKusick 			return (-((cg + 1) * sblock.fs_fpg - sblock.fs_frag));
1182460ed610SKirk McKusick 		baseblk = dtogd(&sblock, blkno + j);
11834336716bSAdrian Chadd 		for (k = 0; k < frags; k++) {
1184460ed610SKirk McKusick 			setbmap(blkno + j + k);
11854336716bSAdrian Chadd 			clrbit(cg_blksfree(cgp), baseblk + k);
11864336716bSAdrian Chadd 		}
11874336716bSAdrian Chadd 		n_blks += frags;
11884336716bSAdrian Chadd 		if (frags == sblock.fs_frag)
11894336716bSAdrian Chadd 			cgp->cg_cs.cs_nbfree--;
11904336716bSAdrian Chadd 		else
11914336716bSAdrian Chadd 			cgp->cg_cs.cs_nffree -= frags;
11928ebae128SKirk McKusick 		cgdirty(cgbp);
1193460ed610SKirk McKusick 		return (blkno + j);
11944336716bSAdrian Chadd 	}
11954336716bSAdrian Chadd 	return (0);
11964336716bSAdrian Chadd }
11974336716bSAdrian Chadd 
11984336716bSAdrian Chadd /*
1199101a9ac0SKirk McKusick  * Check whether a file size is within the limits for the filesystem.
1200101a9ac0SKirk McKusick  * Return 1 when valid and 0 when too big.
1201101a9ac0SKirk McKusick  *
1202101a9ac0SKirk McKusick  * This should match the file size limit in ffs_mountfs().
1203101a9ac0SKirk McKusick  */
1204101a9ac0SKirk McKusick int
chkfilesize(mode_t mode,u_int64_t filesize)1205101a9ac0SKirk McKusick chkfilesize(mode_t mode, u_int64_t filesize)
1206101a9ac0SKirk McKusick {
1207101a9ac0SKirk McKusick 	u_int64_t kernmaxfilesize;
1208101a9ac0SKirk McKusick 
1209101a9ac0SKirk McKusick 	if (sblock.fs_magic == FS_UFS1_MAGIC)
1210101a9ac0SKirk McKusick 		kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
1211101a9ac0SKirk McKusick 	else
1212101a9ac0SKirk McKusick 		kernmaxfilesize = sblock.fs_maxfilesize;
1213101a9ac0SKirk McKusick 	if (filesize > kernmaxfilesize ||
1214101a9ac0SKirk McKusick 	    filesize > sblock.fs_maxfilesize ||
1215101a9ac0SKirk McKusick 	    (mode == IFDIR && filesize > MAXDIRSIZE)) {
1216101a9ac0SKirk McKusick 		if (debug)
1217101a9ac0SKirk McKusick 			printf("bad file size %ju:", (uintmax_t)filesize);
1218101a9ac0SKirk McKusick 		return (0);
1219101a9ac0SKirk McKusick 	}
1220101a9ac0SKirk McKusick 	return (1);
1221101a9ac0SKirk McKusick }
1222101a9ac0SKirk McKusick 
1223101a9ac0SKirk McKusick /*
12247180f1abSKirk McKusick  * Slow down IO so as to leave some disk bandwidth for other processes
12254336716bSAdrian Chadd  */
12264336716bSAdrian Chadd void
slowio_start()12279d580d7cSIan Dowse slowio_start()
12289d580d7cSIan Dowse {
12299d580d7cSIan Dowse 
123008983aeeSScott Long 	/* Delay one in every 8 operations */
12319d580d7cSIan Dowse 	slowio_pollcnt = (slowio_pollcnt + 1) & 7;
12329d580d7cSIan Dowse 	if (slowio_pollcnt == 0) {
12339d580d7cSIan Dowse 		gettimeofday(&slowio_starttime, NULL);
12349d580d7cSIan Dowse 	}
12359d580d7cSIan Dowse }
12369d580d7cSIan Dowse 
12379d580d7cSIan Dowse void
slowio_end()12389d580d7cSIan Dowse slowio_end()
12399d580d7cSIan Dowse {
12409d580d7cSIan Dowse 	struct timeval tv;
12419d580d7cSIan Dowse 	int delay_usec;
12429d580d7cSIan Dowse 
12439d580d7cSIan Dowse 	if (slowio_pollcnt != 0)
12449d580d7cSIan Dowse 		return;
12459d580d7cSIan Dowse 
12469d580d7cSIan Dowse 	/* Update the slowdown interval. */
12479d580d7cSIan Dowse 	gettimeofday(&tv, NULL);
12489d580d7cSIan Dowse 	delay_usec = (tv.tv_sec - slowio_starttime.tv_sec) * 1000000 +
12499d580d7cSIan Dowse 	    (tv.tv_usec - slowio_starttime.tv_usec);
12509d580d7cSIan Dowse 	if (delay_usec < 64)
12519d580d7cSIan Dowse 		delay_usec = 64;
125208983aeeSScott Long 	if (delay_usec > 2500000)
125308983aeeSScott Long 		delay_usec = 2500000;
12549d580d7cSIan Dowse 	slowio_delay_usec = (slowio_delay_usec * 63 + delay_usec) >> 6;
125508983aeeSScott Long 	/* delay by 8 times the average IO delay */
125608983aeeSScott Long 	if (slowio_delay_usec > 64)
125708983aeeSScott Long 		usleep(slowio_delay_usec * 8);
12589d580d7cSIan Dowse }
12599d580d7cSIan Dowse 
12604336716bSAdrian Chadd /*
12614336716bSAdrian Chadd  * Find a pathname
12624336716bSAdrian Chadd  */
12634336716bSAdrian Chadd void
getpathname(char * namebuf,ino_t curdir,ino_t ino)1264b70cd7eeSWarner Losh getpathname(char *namebuf, ino_t curdir, ino_t ino)
12654336716bSAdrian Chadd {
12664336716bSAdrian Chadd 	int len;
12673d438ad6SDavid E. O'Brien 	char *cp;
12685cc52631SKirk McKusick 	struct inode ip;
12694336716bSAdrian Chadd 	struct inodesc idesc;
12704336716bSAdrian Chadd 	static int busy = 0;
12714336716bSAdrian Chadd 
12721dc349abSEd Maste 	if (curdir == ino && ino == UFS_ROOTINO) {
12734336716bSAdrian Chadd 		(void)strcpy(namebuf, "/");
12744336716bSAdrian Chadd 		return;
12754336716bSAdrian Chadd 	}
1276af6726e6SDon Lewis 	if (busy || !INO_IS_DVALID(curdir)) {
12774336716bSAdrian Chadd 		(void)strcpy(namebuf, "?");
12784336716bSAdrian Chadd 		return;
12794336716bSAdrian Chadd 	}
12804336716bSAdrian Chadd 	busy = 1;
12814336716bSAdrian Chadd 	memset(&idesc, 0, sizeof(struct inodesc));
12824336716bSAdrian Chadd 	idesc.id_type = DATA;
12834336716bSAdrian Chadd 	idesc.id_fix = IGNORE;
12844336716bSAdrian Chadd 	cp = &namebuf[MAXPATHLEN - 1];
12854336716bSAdrian Chadd 	*cp = '\0';
12864336716bSAdrian Chadd 	if (curdir != ino) {
12874336716bSAdrian Chadd 		idesc.id_parent = curdir;
12884336716bSAdrian Chadd 		goto namelookup;
12894336716bSAdrian Chadd 	}
12901dc349abSEd Maste 	while (ino != UFS_ROOTINO) {
12914336716bSAdrian Chadd 		idesc.id_number = ino;
12924336716bSAdrian Chadd 		idesc.id_func = findino;
1293599304a4SPoul-Henning Kamp 		idesc.id_name = strdup("..");
12945cc52631SKirk McKusick 		ginode(ino, &ip);
12955cc52631SKirk McKusick 		if ((ckinode(ip.i_dp, &idesc) & FOUND) == 0) {
12965cc52631SKirk McKusick 			irelse(&ip);
1297e5d0d1c5SKirk McKusick 			free(idesc.id_name);
12984336716bSAdrian Chadd 			break;
12995cc52631SKirk McKusick 		}
13005cc52631SKirk McKusick 		irelse(&ip);
1301e5d0d1c5SKirk McKusick 		free(idesc.id_name);
13024336716bSAdrian Chadd 	namelookup:
13034336716bSAdrian Chadd 		idesc.id_number = idesc.id_parent;
13044336716bSAdrian Chadd 		idesc.id_parent = ino;
13054336716bSAdrian Chadd 		idesc.id_func = findname;
13064336716bSAdrian Chadd 		idesc.id_name = namebuf;
13075cc52631SKirk McKusick 		ginode(idesc.id_number, &ip);
13085cc52631SKirk McKusick 		if ((ckinode(ip.i_dp, &idesc) & FOUND) == 0) {
13095cc52631SKirk McKusick 			irelse(&ip);
13104336716bSAdrian Chadd 			break;
13115cc52631SKirk McKusick 		}
13125cc52631SKirk McKusick 		irelse(&ip);
13134336716bSAdrian Chadd 		len = strlen(namebuf);
13144336716bSAdrian Chadd 		cp -= len;
13154336716bSAdrian Chadd 		memmove(cp, namebuf, (size_t)len);
13164336716bSAdrian Chadd 		*--cp = '/';
13170ecf59f6SConrad Meyer 		if (cp < &namebuf[UFS_MAXNAMLEN])
13184336716bSAdrian Chadd 			break;
13194336716bSAdrian Chadd 		ino = idesc.id_number;
13204336716bSAdrian Chadd 	}
13214336716bSAdrian Chadd 	busy = 0;
13221dc349abSEd Maste 	if (ino != UFS_ROOTINO)
13234336716bSAdrian Chadd 		*--cp = '?';
13244336716bSAdrian Chadd 	memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
13254336716bSAdrian Chadd }
13264336716bSAdrian Chadd 
13274336716bSAdrian Chadd void
catch(int sig __unused)1328599304a4SPoul-Henning Kamp catch(int sig __unused)
13294336716bSAdrian Chadd {
1330381ee4c2SPoul-Henning Kamp 
13314336716bSAdrian Chadd 	ckfini(0);
13324336716bSAdrian Chadd 	exit(12);
13334336716bSAdrian Chadd }
13344336716bSAdrian Chadd 
13354336716bSAdrian Chadd /*
13364336716bSAdrian Chadd  * When preening, allow a single quit to signal
13374336716bSAdrian Chadd  * a special exit after file system checks complete
13384336716bSAdrian Chadd  * so that reboot sequence may be interrupted.
13394336716bSAdrian Chadd  */
13404336716bSAdrian Chadd void
catchquit(int sig __unused)1341599304a4SPoul-Henning Kamp catchquit(int sig __unused)
13424336716bSAdrian Chadd {
13434336716bSAdrian Chadd 	printf("returning to single-user after file system check\n");
13444336716bSAdrian Chadd 	returntosingle = 1;
13454336716bSAdrian Chadd 	(void)signal(SIGQUIT, SIG_DFL);
13464336716bSAdrian Chadd }
13474336716bSAdrian Chadd 
13484336716bSAdrian Chadd /*
13494336716bSAdrian Chadd  * determine whether an inode should be fixed.
13504336716bSAdrian Chadd  */
13514336716bSAdrian Chadd int
dofix(struct inodesc * idesc,const char * msg)1352599304a4SPoul-Henning Kamp dofix(struct inodesc *idesc, const char *msg)
13534336716bSAdrian Chadd {
13544336716bSAdrian Chadd 
13554336716bSAdrian Chadd 	switch (idesc->id_fix) {
13564336716bSAdrian Chadd 
13574336716bSAdrian Chadd 	case DONTKNOW:
13584336716bSAdrian Chadd 		if (idesc->id_type == DATA)
13594336716bSAdrian Chadd 			direrror(idesc->id_number, msg);
13604336716bSAdrian Chadd 		else
13615979df34SKris Kennaway 			pwarn("%s", msg);
13624336716bSAdrian Chadd 		if (preen) {
13634336716bSAdrian Chadd 			printf(" (SALVAGED)\n");
13644336716bSAdrian Chadd 			idesc->id_fix = FIX;
13654336716bSAdrian Chadd 			return (ALTERED);
13664336716bSAdrian Chadd 		}
13674336716bSAdrian Chadd 		if (reply("SALVAGE") == 0) {
13684336716bSAdrian Chadd 			idesc->id_fix = NOFIX;
13694336716bSAdrian Chadd 			return (0);
13704336716bSAdrian Chadd 		}
13714336716bSAdrian Chadd 		idesc->id_fix = FIX;
13724336716bSAdrian Chadd 		return (ALTERED);
13734336716bSAdrian Chadd 
13744336716bSAdrian Chadd 	case FIX:
13754336716bSAdrian Chadd 		return (ALTERED);
13764336716bSAdrian Chadd 
13774336716bSAdrian Chadd 	case NOFIX:
13784336716bSAdrian Chadd 	case IGNORE:
13794336716bSAdrian Chadd 		return (0);
13804336716bSAdrian Chadd 
13814336716bSAdrian Chadd 	default:
13824336716bSAdrian Chadd 		errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
13834336716bSAdrian Chadd 	}
13844336716bSAdrian Chadd 	/* NOTREACHED */
13854336716bSAdrian Chadd 	return (0);
13864336716bSAdrian Chadd }
13874336716bSAdrian Chadd 
13884336716bSAdrian Chadd #include <stdarg.h>
13894336716bSAdrian Chadd 
13904336716bSAdrian Chadd /*
13915cc52631SKirk McKusick  * Print details about a buffer.
13925cc52631SKirk McKusick  */
1393460ed610SKirk McKusick void
prtbuf(struct bufarea * bp,const char * fmt,...)1394460ed610SKirk McKusick prtbuf(struct bufarea *bp, const char *fmt, ...)
13955cc52631SKirk McKusick {
1396460ed610SKirk McKusick 	va_list ap;
1397460ed610SKirk McKusick 	va_start(ap, fmt);
1398460ed610SKirk McKusick 	if (preen)
1399460ed610SKirk McKusick 		(void)fprintf(stdout, "%s: ", cdevname);
1400460ed610SKirk McKusick 	(void)vfprintf(stdout, fmt, ap);
1401460ed610SKirk McKusick 	va_end(ap);
1402460ed610SKirk McKusick 	printf(": bp %p, type %s, bno %jd, size %d, refcnt %d, flags %s, "
1403460ed610SKirk McKusick 	    "index %jd\n", bp, BT_BUFTYPE(bp->b_type), (intmax_t) bp->b_bno,
1404460ed610SKirk McKusick 	    bp->b_size, bp->b_refcnt, bp->b_flags & B_DIRTY ? "dirty" : "clean",
1405460ed610SKirk McKusick 	    (intmax_t) bp->b_index);
14065cc52631SKirk McKusick }
14075cc52631SKirk McKusick 
14085cc52631SKirk McKusick /*
14094b85a12fSUlrich Spörlein  * An unexpected inconsistency occurred.
14104336716bSAdrian Chadd  * Die if preening or file system is running with soft dependency protocol,
14114336716bSAdrian Chadd  * otherwise just print message and continue.
14124336716bSAdrian Chadd  */
14134336716bSAdrian Chadd void
pfatal(const char * fmt,...)14144336716bSAdrian Chadd pfatal(const char *fmt, ...)
14154336716bSAdrian Chadd {
14164336716bSAdrian Chadd 	va_list ap;
14174336716bSAdrian Chadd 	va_start(ap, fmt);
14184336716bSAdrian Chadd 	if (!preen) {
141915fca934SKirk McKusick 		(void)vfprintf(stdout, fmt, ap);
14204336716bSAdrian Chadd 		va_end(ap);
14214336716bSAdrian Chadd 		if (usedsoftdep)
142215fca934SKirk McKusick 			(void)fprintf(stdout,
14234336716bSAdrian Chadd 			    "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
142438375c40SKirk McKusick 		/*
142538375c40SKirk McKusick 		 * Force foreground fsck to clean up inconsistency.
142638375c40SKirk McKusick 		 */
142738375c40SKirk McKusick 		if (bkgrdflag) {
142838375c40SKirk McKusick 			cmd.value = FS_NEEDSFSCK;
142938375c40SKirk McKusick 			cmd.size = 1;
143038375c40SKirk McKusick 			if (sysctlbyname("vfs.ffs.setflags", 0, 0,
143138375c40SKirk McKusick 			    &cmd, sizeof cmd) == -1)
143238375c40SKirk McKusick 				pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
143315fca934SKirk McKusick 			fprintf(stdout, "CANNOT RUN IN BACKGROUND\n");
143438375c40SKirk McKusick 			ckfini(0);
143538375c40SKirk McKusick 			exit(EEXIT);
143638375c40SKirk McKusick 		}
14374336716bSAdrian Chadd 		return;
14384336716bSAdrian Chadd 	}
14394336716bSAdrian Chadd 	if (cdevname == NULL)
1440599304a4SPoul-Henning Kamp 		cdevname = strdup("fsck");
144115fca934SKirk McKusick 	(void)fprintf(stdout, "%s: ", cdevname);
144215fca934SKirk McKusick 	(void)vfprintf(stdout, fmt, ap);
144315fca934SKirk McKusick 	(void)fprintf(stdout,
14444336716bSAdrian Chadd 	    "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
14454336716bSAdrian Chadd 	    cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
144638375c40SKirk McKusick 	/*
144738375c40SKirk McKusick 	 * Force foreground fsck to clean up inconsistency.
144838375c40SKirk McKusick 	 */
144938375c40SKirk McKusick 	if (bkgrdflag) {
145038375c40SKirk McKusick 		cmd.value = FS_NEEDSFSCK;
145138375c40SKirk McKusick 		cmd.size = 1;
145238375c40SKirk McKusick 		if (sysctlbyname("vfs.ffs.setflags", 0, 0,
145338375c40SKirk McKusick 		    &cmd, sizeof cmd) == -1)
145438375c40SKirk McKusick 			pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
145538375c40SKirk McKusick 	}
14564336716bSAdrian Chadd 	ckfini(0);
14574336716bSAdrian Chadd 	exit(EEXIT);
14584336716bSAdrian Chadd }
14594336716bSAdrian Chadd 
14604336716bSAdrian Chadd /*
14614336716bSAdrian Chadd  * Pwarn just prints a message when not preening or running soft dependency
14624336716bSAdrian Chadd  * protocol, or a warning (preceded by filename) when preening.
14634336716bSAdrian Chadd  */
14644336716bSAdrian Chadd void
pwarn(const char * fmt,...)14654336716bSAdrian Chadd pwarn(const char *fmt, ...)
14664336716bSAdrian Chadd {
14674336716bSAdrian Chadd 	va_list ap;
14684336716bSAdrian Chadd 	va_start(ap, fmt);
14694336716bSAdrian Chadd 	if (preen)
147015fca934SKirk McKusick 		(void)fprintf(stdout, "%s: ", cdevname);
147115fca934SKirk McKusick 	(void)vfprintf(stdout, fmt, ap);
14724336716bSAdrian Chadd 	va_end(ap);
14734336716bSAdrian Chadd }
14744336716bSAdrian Chadd 
14754336716bSAdrian Chadd /*
14764336716bSAdrian Chadd  * Stub for routines from kernel.
14774336716bSAdrian Chadd  */
14784336716bSAdrian Chadd void
panic(const char * fmt,...)14794336716bSAdrian Chadd panic(const char *fmt, ...)
14804336716bSAdrian Chadd {
14814336716bSAdrian Chadd 	va_list ap;
14824336716bSAdrian Chadd 	va_start(ap, fmt);
14834336716bSAdrian Chadd 	pfatal("INTERNAL INCONSISTENCY:");
148415fca934SKirk McKusick 	(void)vfprintf(stdout, fmt, ap);
14854336716bSAdrian Chadd 	va_end(ap);
14864336716bSAdrian Chadd 	exit(EEXIT);
14874336716bSAdrian Chadd }
1488