aboutsummaryrefslogtreecommitdiff
path: root/sbin/fsck_ffs/fsutil.c
blob: 77571deb38f46d964ec19630f5647be760710702 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
/*-
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 1980, 1986, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#if 0
#ifndef lint
static const char sccsid[] = "@(#)utilities.c	8.6 (Berkeley) 5/19/95";
#endif /* not lint */
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/disk.h>
#include <sys/disklabel.h>
#include <sys/ioctl.h>
#include <sys/stat.h>

#include <ufs/ufs/dinode.h>
#include <ufs/ufs/dir.h>
#include <ufs/ffs/fs.h>

#include <err.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <fstab.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <libufs.h>

#include "fsck.h"

int		sujrecovery = 0;

static struct bufarea *allocbuf(const char *);
static void cg_write(struct bufarea *);
static void slowio_start(void);
static void slowio_end(void);
static void printIOstats(void);
static void prtbuf(const char *, struct bufarea *);

static long diskreads, totaldiskreads, totalreads; /* Disk cache statistics */
static struct timespec startpass, finishpass;
struct timeval slowio_starttime;
int slowio_delay_usec = 10000;	/* Initial IO delay for background fsck */
int slowio_pollcnt;
static struct bufarea cgblk;	/* backup buffer for cylinder group blocks */
static TAILQ_HEAD(bufqueue, bufarea) bufqueuehd; /* head of buffer cache LRU */
static LIST_HEAD(bufhash, bufarea) bufhashhd[HASHSIZE]; /* buffer hash list */
static int numbufs;				/* size of buffer cache */
static int cachelookups;			/* number of cache lookups */
static int cachereads;				/* number of cache reads */
static struct bufarea *cgbufs;	/* header for cylinder group cache */
static int flushtries;		/* number of tries to reclaim memory */

char *buftype[BT_NUMBUFTYPES] = BT_NAMES;

void
fsutilinit(void)
{
	diskreads = totaldiskreads = totalreads = 0;
	bzero(&startpass, sizeof(struct timespec));
	bzero(&finishpass, sizeof(struct timespec));
	bzero(&slowio_starttime, sizeof(struct timeval));
	slowio_delay_usec = 10000;
	slowio_pollcnt = 0;
	flushtries = 0;
}

int
ftypeok(union dinode *dp)
{
	switch (DIP(dp, di_mode) & IFMT) {

	case IFDIR:
	case IFREG:
	case IFBLK:
	case IFCHR:
	case IFLNK:
	case IFSOCK:
	case IFIFO:
		return (1);

	default:
		if (debug)
			printf("bad file type 0%o\n", DIP(dp, di_mode));
		return (0);
	}
}

int
reply(const char *question)
{
	int persevere;
	char c;

	if (preen)
		pfatal("INTERNAL ERROR: GOT TO reply()");
	persevere = !strcmp(question, "CONTINUE");
	printf("\n");
	if (!persevere && (nflag || (fswritefd < 0 && bkgrdflag == 0))) {
		printf("%s? no\n\n", question);
		resolved = 0;
		return (0);
	}
	if (yflag || (persevere && nflag)) {
		printf("%s? yes\n\n", question);
		return (1);
	}
	do	{
		printf("%s? [yn] ", question);
		(void) fflush(stdout);
		c = getc(stdin);
		while (c != '\n' && getc(stdin) != '\n') {
			if (feof(stdin)) {
				resolved = 0;
				return (0);
			}
		}
	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
	printf("\n");
	if (c == 'y' || c == 'Y')
		return (1);
	resolved = 0;
	return (0);
}

/*
 * Look up state information for an inode.
 */
struct inostat *
inoinfo(ino_t inum)
{
	static struct inostat unallocated = { USTATE, 0, 0 };
	struct inostatlist *ilp;
	int iloff;

	if (inum > maxino)
		errx(EEXIT, "inoinfo: inumber %ju out of range",
		    (uintmax_t)inum);
	ilp = &inostathead[inum / sblock.fs_ipg];
	iloff = inum % sblock.fs_ipg;
	if (iloff >= ilp->il_numalloced)
		return (&unallocated);
	return (&ilp->il_stat[iloff]);
}

/*
 * Malloc buffers and set up cache.
 */
void
bufinit(void)
{
	int i;

	pdirbp = (struct bufarea *)0;
	bzero(&cgblk, sizeof(struct bufarea));
	cgblk.b_un.b_buf = Malloc((unsigned int)sblock.fs_bsize);
	if (cgblk.b_un.b_buf == NULL)
		errx(EEXIT, "Initial malloc(%d) failed", sblock.fs_bsize);
	initbarea(&cgblk, BT_CYLGRP);
	cgbufs = NULL;
	numbufs = cachelookups = cachereads = 0;
	TAILQ_INIT(&bufqueuehd);
	for (i = 0; i < HASHSIZE; i++)
		LIST_INIT(&bufhashhd[i]);
	for (i = 0; i < BT_NUMBUFTYPES; i++) {
		readtime[i].tv_sec = totalreadtime[i].tv_sec = 0;
		readtime[i].tv_nsec = totalreadtime[i].tv_nsec = 0;
		readcnt[i] = totalreadcnt[i] = 0;
	}
}

static struct bufarea *
allocbuf(const char *failreason)
{
	struct bufarea *bp;
	char *bufp;

	bp = (struct bufarea *)Malloc(sizeof(struct bufarea));
	bufp = Malloc((unsigned int)sblock.fs_bsize);
	if (bp == NULL || bufp == NULL) {
		errx(EEXIT, "%s", failreason);
		/* NOTREACHED */
	}
	numbufs++;
	bp->b_un.b_buf = bufp;
	TAILQ_INSERT_HEAD(&bufqueuehd, bp, b_list);
	initbarea(bp, BT_UNKNOWN);
	return (bp);
}

/*
 * Manage cylinder group buffers.
 *
 * Use getblk() here rather than cgget() because the cylinder group
 * may be corrupted but we want it anyway so we can fix it.
 */
static struct bufarea *cgbufs;	/* header for cylinder group cache */
static int flushtries;		/* number of tries to reclaim memory */

struct bufarea *
cglookup(int cg)
{
	struct bufarea *cgbp;
	struct cg *cgp;

	if ((unsigned) cg >= sblock.fs_ncg)
		errx(EEXIT, "cglookup: out of range cylinder group %d", cg);
	if (cgbufs == NULL) {
		cgbufs = calloc(sblock.fs_ncg, sizeof(struct bufarea));
		if (cgbufs == NULL)
			errx(EEXIT, "Cannot allocate cylinder group buffers");
	}
	cgbp = &cgbufs[cg];
	if (cgbp->b_un.b_cg != NULL)
		return (cgbp);
	cgp = NULL;
	if (flushtries == 0)
		cgp = Malloc((unsigned int)sblock.fs_cgsize);
	if (cgp == NULL) {
		if (sujrecovery)
			errx(EEXIT,"Ran out of memory during journal recovery");
		getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
		return (&cgblk);
	}
	cgbp->b_un.b_cg = cgp;
	initbarea(cgbp, BT_CYLGRP);
	getblk(cgbp, cgtod(&sblock, cg), sblock.fs_cgsize);
	return (cgbp);
}

/*
 * Mark a cylinder group buffer as dirty.
 * Update its check-hash if they are enabled.
 */
void
cgdirty(struct bufarea *cgbp)
{
	struct cg *cg;

	cg = cgbp->b_un.b_cg;
	if ((sblock.fs_metackhash & CK_CYLGRP) != 0) {
		cg->cg_ckhash = 0;
		cg->cg_ckhash =
		    calculate_crc32c(~0L, (void *)cg, sblock.fs_cgsize);
	}
	dirty(cgbp);
}

/*
 * Attempt to flush a cylinder group cache entry.
 * Return whether the flush was successful.
 */
int
flushentry(void)
{
	struct bufarea *cgbp;

	if (sujrecovery || flushtries == sblock.fs_ncg || cgbufs == NULL)
		return (0);
	cgbp = &cgbufs[flushtries++];
	if (cgbp->b_un.b_cg == NULL)
		return (0);
	flush(fswritefd, cgbp);
	free(cgbp->b_un.b_buf);
	cgbp->b_un.b_buf = NULL;
	return (1);
}

/*
 * Manage a cache of directory blocks.
 */
struct bufarea *
getdatablk(ufs2_daddr_t blkno, long size, int type)
{
	struct bufarea *bp;
	struct bufhash *bhdp;

	cachelookups++;
	/* If out of range, return empty buffer with b_err == -1 */
	if (type != BT_INODES && chkrange(blkno, size / sblock.fs_fsize)) {
		blkno = -1;
		type = BT_EMPTY;
	}
	bhdp = &bufhashhd[HASH(blkno)];
	LIST_FOREACH(bp, bhdp, b_hash)
		if (bp->b_bno == fsbtodb(&sblock, blkno)) {
			if (debug && bp->b_size != size) {
				prtbuf("getdatablk: size mismatch", bp);
				pfatal("getdatablk: b_size %d != size %ld\n",
				    bp->b_size, size);
			}
			goto foundit;
		}
	/*
	 * Move long-term busy buffer back to the front of the LRU so we 
	 * do not endless inspect them for recycling.
	 */
	bp = TAILQ_LAST(&bufqueuehd, bufqueue);
	if (bp != NULL && bp->b_refcnt != 0) {
		TAILQ_REMOVE(&bufqueuehd, bp, b_list);
		TAILQ_INSERT_HEAD(&bufqueuehd, bp, b_list);
	}
	/*
	 * Allocate up to the minimum number of buffers before
	 * considering recycling any of them.
	 */
	if (size > sblock.fs_bsize)
		errx(EEXIT, "Excessive buffer size %ld > %d\n", size,
		    sblock.fs_bsize);
	if (numbufs < MINBUFS) {
		bp = allocbuf("cannot create minimal buffer pool");
	} else if (sujrecovery) {
		/*
		 * SUJ recovery does not want anything written until it 
		 * has successfully completed (so it can fail back to
		 * full fsck). Thus, we can only recycle clean buffers.
		 */
		TAILQ_FOREACH_REVERSE(bp, &bufqueuehd, bufqueue, b_list)
			if ((bp->b_flags & B_DIRTY) == 0 && bp->b_refcnt == 0)
				break;
		if (bp == NULL)
			bp = allocbuf("Ran out of memory during "
			    "journal recovery");
		else
			LIST_REMOVE(bp, b_hash);
	} else {
		/*
		 * Recycle oldest non-busy buffer.
		 */
		TAILQ_FOREACH_REVERSE(bp, &bufqueuehd, bufqueue, b_list)
			if (bp->b_refcnt == 0)
				break;
		if (bp == NULL)
			bp = allocbuf("Ran out of memory for buffers");
		else
			LIST_REMOVE(bp, b_hash);
	}
	flush(fswritefd, bp);
	bp->b_type = type;
	LIST_INSERT_HEAD(bhdp, bp, b_hash);
	getblk(bp, blkno, size);
	cachereads++;
	/* fall through */
foundit:
	if (debug && bp->b_type != type) {
		printf("getdatablk: buffer type changed to %s",
		    BT_BUFTYPE(type));
		prtbuf("", bp);
	}
	TAILQ_REMOVE(&bufqueuehd, bp, b_list);
	TAILQ_INSERT_HEAD(&bufqueuehd, bp, b_list);
	if (bp->b_errs == 0)
		bp->b_refcnt++;
	return (bp);
}

void
getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
{
	ufs2_daddr_t dblk;
	struct timespec start, finish;

	dblk = fsbtodb(&sblock, blk);
	if (bp->b_bno == dblk) {
		totalreads++;
	} else {
		if (debug) {
			readcnt[bp->b_type]++;
			clock_gettime(CLOCK_REALTIME_PRECISE, &start);
		}
		if (bp->b_type != BT_EMPTY)
			bp->b_errs =
			    blread(fsreadfd, bp->b_un.b_buf, dblk, size);
		else
			bp->b_errs = -1;
		if (debug) {
			clock_gettime(CLOCK_REALTIME_PRECISE, &finish);
			timespecsub(&finish, &start, &finish);
			timespecadd(&readtime[bp->b_type], &finish,
			    &readtime[bp->b_type]);
		}
		bp->b_bno = dblk;
		bp->b_size = size;
	}
}

void
brelse(struct bufarea *bp)
{

	if (bp->b_refcnt <= 0)
		prtbuf("brelse: buffer with negative reference count", bp);
	bp->b_refcnt--;
}

void
flush(int fd, struct bufarea *bp)
{
	struct inode ip;

	if ((bp->b_flags & B_DIRTY) == 0)
		return;
	bp->b_flags &= ~B_DIRTY;
	if (fswritefd < 0) {
		pfatal("WRITING IN READ_ONLY MODE.\n");
		return;
	}
	if (bp->b_errs != 0)
		pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
		    (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
		    (long long)bp->b_bno);
	bp->b_errs = 0;
	/*
	 * Write using the appropriate function.
	 */
	switch (bp->b_type) {
	case BT_SUPERBLK:
		if (bp != &sblk)
			pfatal("BUFFER %p DOES NOT MATCH SBLK %p\n",
			    bp, &sblk);
		if (sbput(fd, bp->b_un.b_fs, 0) == 0)
			fsmodified = 1;
		break;
	case BT_CYLGRP:
		if (sujrecovery)
			cg_write(bp);
		if (cgput(fswritefd, &sblock, bp->b_un.b_cg) == 0)
			fsmodified = 1;
		break;
	case BT_INODES:
		if (debug && sblock.fs_magic == FS_UFS2_MAGIC) {
			struct ufs2_dinode *dp = bp->b_un.b_dinode2;
			int i;

			for (i = 0; i < INOPB(&sblock); dp++, i++) {
				if (ffs_verify_dinode_ckhash(&sblock, dp) == 0)
					continue;
				pwarn("flush: INODE CHECK-HASH FAILED");
				ip.i_bp = bp;
				ip.i_dp = (union dinode *)dp;
				ip.i_number = bp->b_index + i;
				prtinode(&ip);
				if (preen || reply("FIX") != 0) {
					if (preen)
						printf(" (FIXED)\n");
					ffs_update_dinode_ckhash(&sblock, dp);
					inodirty(&ip);
				}
			}
		}
		/* FALLTHROUGH */
	default:
		blwrite(fd, bp->b_un.b_buf, bp->b_bno, bp->b_size);
		break;
	}
}

/*
 * Journaled soft updates does not maintain cylinder group summary
 * information during cleanup, so this routine recalculates the summary
 * information and updates the superblock summary in preparation for
 * writing out the cylinder group.
 */
static void
cg_write(struct bufarea *bp)
{
	ufs1_daddr_t fragno, cgbno, maxbno;
	u_int8_t *blksfree;
	struct cg *cgp;
	int blk;
	int i;

	/*
	 * Fix the frag and cluster summary.
	 */
	cgp = bp->b_un.b_cg;
	cgp->cg_cs.cs_nbfree = 0;
	cgp->cg_cs.cs_nffree = 0;
	bzero(&cgp->cg_frsum, sizeof(cgp->cg_frsum));
	maxbno = fragstoblks(&sblock, sblock.fs_fpg);
	if (sblock.fs_contigsumsize > 0) {
		for (i = 1; i <= sblock.fs_contigsumsize; i++)
			cg_clustersum(cgp)[i] = 0;
		bzero(cg_clustersfree(cgp), howmany(maxbno, CHAR_BIT));
	}
	blksfree = cg_blksfree(cgp);
	for (cgbno = 0; cgbno < maxbno; cgbno++) {
		if (ffs_isfreeblock(&sblock, blksfree, cgbno))
			continue;
		if (ffs_isblock(&sblock, blksfree, cgbno)) {
			ffs_clusteracct(&sblock, cgp, cgbno, 1);
			cgp->cg_cs.cs_nbfree++;
			continue;
		}
		fragno = blkstofrags(&sblock, cgbno);
		blk = blkmap(&sblock, blksfree, fragno);
		ffs_fragacct(&sblock, blk, cgp->cg_frsum, 1);
		for (i = 0; i < sblock.fs_frag; i++)
			if (isset(blksfree, fragno + i))
				cgp->cg_cs.cs_nffree++;
	}
	/*
	 * Update the superblock cg summary from our now correct values
	 * before writing the block.
	 */
	sblock.fs_cs(&sblock, cgp->cg_cgx) = cgp->cg_cs;
}

void
rwerror(const char *mesg, ufs2_daddr_t blk)
{

	if (bkgrdcheck)
		exit(EEXIT);
	if (preen == 0)
		printf("\n");
	pfatal("CANNOT %s: %ld", mesg, (long)blk);
	if (reply("CONTINUE") == 0)
		exit(EEXIT);
}

void
ckfini(int markclean)
{
	struct bufarea *bp, *nbp;
	int ofsmodified, cnt;

	if (bkgrdflag) {
		unlink(snapname);
		if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) {
			cmd.value = FS_UNCLEAN;
			cmd.size = markclean ? -1 : 1;
			if (sysctlbyname("vfs.ffs.setflags", 0, 0,
			    &cmd, sizeof cmd) == -1)
				rwerror("SET FILE SYSTEM FLAGS", FS_UNCLEAN);
			if (!preen) {
				printf("\n***** FILE SYSTEM MARKED %s *****\n",
				    markclean ? "CLEAN" : "DIRTY");
				if (!markclean)
					rerun = 1;
			}
		} else if (!preen && !markclean) {
			printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
			rerun = 1;
		}
	}
	if (debug && totalreads > 0)
		printf("cache with %d buffers missed %d of %d (%d%%)\n",
		    numbufs, cachereads, cachelookups,
		    (int)(cachereads * 100 / cachelookups));
	if (fswritefd < 0) {
		(void)close(fsreadfd);
		return;
	}
	/*
	 * To remain idempotent with partial truncations the buffers
	 * must be flushed in this order:
	 *  1) cylinder groups (bitmaps)
	 *  2) indirect, directory, external attribute, and data blocks
	 *  3) inode blocks
	 *  4) superblock
	 * This ordering preserves access to the modified pointers
	 * until they are freed.
	 */
	/* Step 1: cylinder groups */
	if (debug)
		printf("Flush Cylinder groups\n");
	if (cgbufs != NULL) {
		for (cnt = 0; cnt < sblock.fs_ncg; cnt++) {
			if (cgbufs[cnt].b_un.b_cg == NULL)
				continue;
			flush(fswritefd, &cgbufs[cnt]);
			free(cgbufs[cnt].b_un.b_cg);
		}
		free(cgbufs);
	}
	flush(fswritefd, &cgblk);
	free(cgblk.b_un.b_buf);
	cnt = 0;
	/* Step 2: indirect, directory, external attribute, and data blocks */
	if (debug)
		printf("Flush indirect, directory, external attribute, "
		    "and data blocks\n");
	if (pdirbp != NULL)
		brelse(pdirbp);
	TAILQ_FOREACH_REVERSE_SAFE(bp, &bufqueuehd, bufqueue, b_list, nbp) {
		switch (bp->b_type) {
		/* These should not be in the buffer cache list */
		case BT_UNKNOWN:
		case BT_SUPERBLK:
		case BT_CYLGRP:
		default:
			prtbuf("ckfini: improper buffer type on cache list",bp);
			continue;
		/* These are the ones to flush in this step */
		case BT_EMPTY:
			if (bp->b_bno >= 0)
				pfatal("Unused BT_EMPTY buffer for block %jd\n",
				    (intmax_t)bp->b_bno);
			/* FALLTHROUGH */
		case BT_LEVEL1:
		case BT_LEVEL2:
		case BT_LEVEL3:
		case BT_EXTATTR:
		case BT_DIRDATA:
		case BT_DATA:
			break;
		/* These are the ones to flush in the next step */
		case BT_INODES:
			continue;
		}
		if (debug && bp->b_refcnt != 0) {
			prtbuf("ckfini: clearing in-use buffer", bp);
			pfatal("ckfini: clearing in-use buffer\n");
		}
		TAILQ_REMOVE(&bufqueuehd, bp, b_list);
		cnt++;
		flush(fswritefd, bp);
		free(bp->b_un.b_buf);
		free((char *)bp);
	}
	/* Step 3: inode blocks */
	if (debug)
		printf("Flush inode blocks\n");
	if (icachebp != NULL)
		brelse(icachebp);
	TAILQ_FOREACH_REVERSE_SAFE(bp, &bufqueuehd, bufqueue, b_list, nbp) {
		if (debug && bp->b_refcnt != 0) {
			prtbuf("ckfini: clearing in-use buffer", bp);
			pfatal("ckfini: clearing in-use buffer\n");
		}
		TAILQ_REMOVE(&bufqueuehd, bp, b_list);
		cnt++;
		flush(fswritefd, bp);
		free(bp->b_un.b_buf);
		free((char *)bp);
	}
	if (numbufs != cnt)
		errx(EEXIT, "panic: lost %d buffers", numbufs - cnt);
	/* Step 4: superblock */
	if (debug)
		printf("Flush the superblock\n");
	flush(fswritefd, &sblk);
	if (havesb && cursnapshot == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
	    sblk.b_bno != sblock.fs_sblockloc / dev_bsize &&
	    !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
		/* Change the write destination to standard superblock */
		sblock.fs_sblockactualloc = sblock.fs_sblockloc;
		sblk.b_bno = sblock.fs_sblockloc / dev_bsize;
		sbdirty();
		flush(fswritefd, &sblk);
	}
	pdirbp = (struct bufarea *)0;
	if (cursnapshot == 0 && sblock.fs_clean != markclean) {
		if ((sblock.fs_clean = markclean) != 0) {
			sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK);
			sblock.fs_pendingblocks = 0;
			sblock.fs_pendinginodes = 0;
		}
		sbdirty();
		ofsmodified = fsmodified;
		flush(fswritefd, &sblk);
		fsmodified = ofsmodified;
		if (!preen) {
			printf("\n***** FILE SYSTEM MARKED %s *****\n",
			    markclean ? "CLEAN" : "DIRTY");
			if (!markclean)
				rerun = 1;
		}
	} else if (!preen) {
		if (markclean) {
			printf("\n***** FILE SYSTEM IS CLEAN *****\n");
		} else {
			printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
			rerun = 1;
		}
	}
	finalIOstats();
	(void)close(fsreadfd);
	(void)close(fswritefd);
}

/*
 * Print out I/O statistics.
 */
void
IOstats(char *what)
{
	int i;

	if (debug == 0)
		return;
	if (diskreads == 0) {
		printf("%s: no I/O\n\n", what);
		return;
	}
	if (startpass.tv_sec == 0)
		startpass = startprog;
	printf("%s: I/O statistics\n", what);
	printIOstats();
	totaldiskreads += diskreads;
	diskreads = 0;
	for (i = 0; i < BT_NUMBUFTYPES; i++) {
		timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
		totalreadcnt[i] += readcnt[i];
		readtime[i].tv_sec = readtime[i].tv_nsec = 0;
		readcnt[i] = 0;
	}
	clock_gettime(CLOCK_REALTIME_PRECISE, &startpass);
}

void
finalIOstats(void)
{
	int i;

	if (debug == 0)
		return;
	printf("Final I/O statistics\n");
	totaldiskreads += diskreads;
	diskreads = totaldiskreads;
	startpass = startprog;
	for (i = 0; i < BT_NUMBUFTYPES; i++) {
		timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
		totalreadcnt[i] += readcnt[i];
		readtime[i] = totalreadtime[i];
		readcnt[i] = totalreadcnt[i];
	}
	printIOstats();
}

static void printIOstats(void)
{
	long long msec, totalmsec;
	int i;

	clock_gettime(CLOCK_REALTIME_PRECISE, &finishpass);
	timespecsub(&finishpass, &startpass, &finishpass);
	printf("Running time: %jd.%03ld sec\n",
		(intmax_t)finishpass.tv_sec, finishpass.tv_nsec / 1000000);
	printf("buffer reads by type:\n");
	for (totalmsec = 0, i = 0; i < BT_NUMBUFTYPES; i++)
		totalmsec += readtime[i].tv_sec * 1000 +
		    readtime[i].tv_nsec / 1000000;
	if (totalmsec == 0)
		totalmsec = 1;
	for (i = 0; i < BT_NUMBUFTYPES; i++) {
		if (readcnt[i] == 0)
			continue;
		msec =
		    readtime[i].tv_sec * 1000 + readtime[i].tv_nsec / 1000000;
		printf("%21s:%8ld %2ld.%ld%% %4jd.%03ld sec %2lld.%lld%%\n",
		    buftype[i], readcnt[i], readcnt[i] * 100 / diskreads,
		    (readcnt[i] * 1000 / diskreads) % 10,
		    (intmax_t)readtime[i].tv_sec, readtime[i].tv_nsec / 1000000,
		    msec * 100 / totalmsec, (msec * 1000 / totalmsec) % 10);
	}
	printf("\n");
}

int
blread(int fd, char *buf, ufs2_daddr_t blk, long size)
{
	char *cp;
	int i, errs;
	off_t offset;

	offset = blk;
	offset *= dev_bsize;
	if (bkgrdflag)
		slowio_start();
	totalreads++;
	diskreads++;
	if (pread(fd, buf, (int)size, offset) == size) {
		if (bkgrdflag)
			slowio_end();
		return (0);
	}

	/*
	 * This is handled specially here instead of in rwerror because
	 * rwerror is used for all sorts of errors, not just true read/write
	 * errors.  It should be refactored and fixed.
	 */
	if (surrender) {
		pfatal("CANNOT READ_BLK: %ld", (long)blk);
		errx(EEXIT, "ABORTING DUE TO READ ERRORS");
	} else
		rwerror("READ BLK", blk);

	errs = 0;
	memset(buf, 0, (size_t)size);
	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
		if (pread(fd, cp, (int)secsize, offset + i) != secsize) {
			if (secsize != dev_bsize && dev_bsize != 1)
				printf(" %jd (%jd),",
				    (intmax_t)(blk * dev_bsize + i) / secsize,
				    (intmax_t)blk + i / dev_bsize);
			else
				printf(" %jd,", (intmax_t)blk + i / dev_bsize);
			errs++;
		}
	}
	printf("\n");
	if (errs)
		resolved = 0;
	return (errs);
}

void
blwrite(int fd, char *buf, ufs2_daddr_t blk, ssize_t size)
{
	int i;
	char *cp;
	off_t offset;

	if (fd < 0)
		return;
	offset = blk;
	offset *= dev_bsize;
	if (pwrite(fd, buf, size, offset) == size) {
		fsmodified = 1;
		return;
	}
	resolved = 0;
	rwerror("WRITE BLK", blk);
	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
		if (pwrite(fd, cp, dev_bsize, offset + i) != dev_bsize)
			printf(" %jd,", (intmax_t)blk + i / dev_bsize);
	printf("\n");
	return;
}

void
blerase(int fd, ufs2_daddr_t blk, long size)
{
	off_t ioarg[2];

	if (fd < 0)
		return;
	ioarg[0] = blk * dev_bsize;
	ioarg[1] = size;
	ioctl(fd, DIOCGDELETE, ioarg);
	/* we don't really care if we succeed or not */
	return;
}

/*
 * Fill a contiguous region with all-zeroes.  Note ZEROBUFSIZE is by
 * definition a multiple of dev_bsize.
 */
void
blzero(int fd, ufs2_daddr_t blk, long size)
{
	static char *zero;
	off_t offset, len;

	if (fd < 0)
		return;
	if (zero == NULL) {
		zero = calloc(ZEROBUFSIZE, 1);
		if (zero == NULL)
			errx(EEXIT, "cannot allocate buffer pool");
	}
	offset = blk * dev_bsize;
	if (lseek(fd, offset, 0) < 0)
		rwerror("SEEK BLK", blk);
	while (size > 0) {
		len = MIN(ZEROBUFSIZE, size);
		if (write(fd, zero, len) != len)
			rwerror("WRITE BLK", blk);
		blk += len / dev_bsize;
		size -= len;
	}
}

/*
 * Verify cylinder group's magic number and other parameters.  If the
 * test fails, offer an option to rebuild the whole cylinder group.
 */
int
check_cgmagic(int cg, struct bufarea *cgbp, int request_rebuild)
{
	struct cg *cgp = cgbp->b_un.b_cg;
	uint32_t cghash, calchash;

	/*
	 * Extended cylinder group checks.
	 */
	calchash = cgp->cg_ckhash;
	if ((sblock.fs_metackhash & CK_CYLGRP) != 0) {
		cghash = cgp->cg_ckhash;
		cgp->cg_ckhash = 0;
		calchash = calculate_crc32c(~0L, (void *)cgp, sblock.fs_cgsize);
		cgp->cg_ckhash = cghash;
	}
	if (cgp->cg_ckhash == calchash &&
	    cg_chkmagic(cgp) &&
	    cgp->cg_cgx == cg &&
	    ((sblock.fs_magic == FS_UFS1_MAGIC &&
	      cgp->cg_old_niblk == sblock.fs_ipg &&
	      cgp->cg_ndblk <= sblock.fs_fpg &&
	      cgp->cg_old_ncyl <= sblock.fs_old_cpg) ||
	     (sblock.fs_magic == FS_UFS2_MAGIC &&
	      cgp->cg_niblk == sblock.fs_ipg &&
	      cgp->cg_ndblk <= sblock.fs_fpg &&
	      cgp->cg_initediblk <= sblock.fs_ipg))) {
		return (1);
	}
	pfatal("CYLINDER GROUP %d: INTEGRITY CHECK FAILED", cg);
	if (!request_rebuild)
		return (0);
	if (!reply("REBUILD CYLINDER GROUP")) {
		printf("YOU WILL NEED TO RERUN FSCK.\n");
		rerun = 1;
		return (1);
	}
	/*
	 * Zero out the cylinder group and then initialize critical fields.
	 * Bit maps and summaries will be recalculated by later passes.
	 */
	memset(cgp, 0, (size_t)sblock.fs_cgsize);
	cgp->cg_magic = CG_MAGIC;
	cgp->cg_cgx = cg;
	cgp->cg_niblk = sblock.fs_ipg;
	cgp->cg_initediblk = MIN(sblock.fs_ipg, 2 * INOPB(&sblock));
	if (cgbase(&sblock, cg) + sblock.fs_fpg < sblock.fs_size)
		cgp->cg_ndblk = sblock.fs_fpg;
	else
		cgp->cg_ndblk = sblock.fs_size - cgbase(&sblock, cg);
	cgp->cg_iusedoff = &cgp->cg_space[0] - (u_char *)(&cgp->cg_firstfield);
	if (sblock.fs_magic == FS_UFS1_MAGIC) {
		cgp->cg_niblk = 0;
		cgp->cg_initediblk = 0;
		cgp->cg_old_ncyl = sblock.fs_old_cpg;
		cgp->cg_old_niblk = sblock.fs_ipg;
		cgp->cg_old_btotoff = cgp->cg_iusedoff;
		cgp->cg_old_boff = cgp->cg_old_btotoff +
		    sblock.fs_old_cpg * sizeof(int32_t);
		cgp->cg_iusedoff = cgp->cg_old_boff +
		    sblock.fs_old_cpg * sizeof(u_int16_t);
	}
	cgp->cg_freeoff = cgp->cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
	cgp->cg_nextfreeoff = cgp->cg_freeoff + howmany(sblock.fs_fpg,CHAR_BIT);
	if (sblock.fs_contigsumsize > 0) {
		cgp->cg_nclusterblks = cgp->cg_ndblk / sblock.fs_frag;
		cgp->cg_clustersumoff =
		    roundup(cgp->cg_nextfreeoff, sizeof(u_int32_t));
		cgp->cg_clustersumoff -= sizeof(u_int32_t);
		cgp->cg_clusteroff = cgp->cg_clustersumoff +
		    (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
		cgp->cg_nextfreeoff = cgp->cg_clusteroff +
		    howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
	}
	cgdirty(cgbp);
	return (0);
}

/*
 * allocate a data block with the specified number of fragments
 */
ufs2_daddr_t
allocblk(long frags)
{
	int i, j, k, cg, baseblk;
	struct bufarea *cgbp;
	struct cg *cgp;

	if (frags <= 0 || frags > sblock.fs_frag)
		return (0);
	for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
		for (j = 0; j <= sblock.fs_frag - frags; j++) {
			if (testbmap(i + j))
				continue;
			for (k = 1; k < frags; k++)
				if (testbmap(i + j + k))
					break;
			if (k < frags) {
				j += k;
				continue;
			}
			cg = dtog(&sblock, i + j);
			cgbp = cglookup(cg);
			cgp = cgbp->b_un.b_cg;
			if (!check_cgmagic(cg, cgbp, 0))
				return (0);
			baseblk = dtogd(&sblock, i + j);
			for (k = 0; k < frags; k++) {
				setbmap(i + j + k);
				clrbit(cg_blksfree(cgp), baseblk + k);
			}
			n_blks += frags;
			if (frags == sblock.fs_frag)
				cgp->cg_cs.cs_nbfree--;
			else
				cgp->cg_cs.cs_nffree -= frags;
			cgdirty(cgbp);
			return (i + j);
		}
	}
	return (0);
}

/*
 * Slow down IO so as to leave some disk bandwidth for other processes
 */
void
slowio_start()
{

	/* Delay one in every 8 operations */
	slowio_pollcnt = (slowio_pollcnt + 1) & 7;
	if (slowio_pollcnt == 0) {
		gettimeofday(&slowio_starttime, NULL);
	}
}

void
slowio_end()
{
	struct timeval tv;
	int delay_usec;

	if (slowio_pollcnt != 0)
		return;

	/* Update the slowdown interval. */
	gettimeofday(&tv, NULL);
	delay_usec = (tv.tv_sec - slowio_starttime.tv_sec) * 1000000 +
	    (tv.tv_usec - slowio_starttime.tv_usec);
	if (delay_usec < 64)
		delay_usec = 64;
	if (delay_usec > 2500000)
		delay_usec = 2500000;
	slowio_delay_usec = (slowio_delay_usec * 63 + delay_usec) >> 6;
	/* delay by 8 times the average IO delay */
	if (slowio_delay_usec > 64)
		usleep(slowio_delay_usec * 8);
}

/*
 * Find a pathname
 */
void
getpathname(char *namebuf, ino_t curdir, ino_t ino)
{
	int len;
	char *cp;
	struct inode ip;
	struct inodesc idesc;
	static int busy = 0;

	if (curdir == ino && ino == UFS_ROOTINO) {
		(void)strcpy(namebuf, "/");
		return;
	}
	if (busy || !INO_IS_DVALID(curdir)) {
		(void)strcpy(namebuf, "?");
		return;
	}
	busy = 1;
	memset(&idesc, 0, sizeof(struct inodesc));
	idesc.id_type = DATA;
	idesc.id_fix = IGNORE;
	cp = &namebuf[MAXPATHLEN - 1];
	*cp = '\0';
	if (curdir != ino) {
		idesc.id_parent = curdir;
		goto namelookup;
	}
	while (ino != UFS_ROOTINO) {
		idesc.id_number = ino;
		idesc.id_func = findino;
		idesc.id_name = strdup("..");
		ginode(ino, &ip);
		if ((ckinode(ip.i_dp, &idesc) & FOUND) == 0) {
			irelse(&ip);
			break;
		}
		irelse(&ip);
	namelookup:
		idesc.id_number = idesc.id_parent;
		idesc.id_parent = ino;
		idesc.id_func = findname;
		idesc.id_name = namebuf;
		ginode(idesc.id_number, &ip);
		if ((ckinode(ip.i_dp, &idesc) & FOUND) == 0) {
			irelse(&ip);
			break;
		}
		irelse(&ip);
		len = strlen(namebuf);
		cp -= len;
		memmove(cp, namebuf, (size_t)len);
		*--cp = '/';
		if (cp < &namebuf[UFS_MAXNAMLEN])
			break;
		ino = idesc.id_number;
	}
	busy = 0;
	if (ino != UFS_ROOTINO)
		*--cp = '?';
	memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
}

void
catch(int sig __unused)
{

	ckfini(0);
	exit(12);
}

/*
 * When preening, allow a single quit to signal
 * a special exit after file system checks complete
 * so that reboot sequence may be interrupted.
 */
void
catchquit(int sig __unused)
{
	printf("returning to single-user after file system check\n");
	returntosingle = 1;
	(void)signal(SIGQUIT, SIG_DFL);
}

/*
 * determine whether an inode should be fixed.
 */
int
dofix(struct inodesc *idesc, const char *msg)
{

	switch (idesc->id_fix) {

	case DONTKNOW:
		if (idesc->id_type == DATA)
			direrror(idesc->id_number, msg);
		else
			pwarn("%s", msg);
		if (preen) {
			printf(" (SALVAGED)\n");
			idesc->id_fix = FIX;
			return (ALTERED);
		}
		if (reply("SALVAGE") == 0) {
			idesc->id_fix = NOFIX;
			return (0);
		}
		idesc->id_fix = FIX;
		return (ALTERED);

	case FIX:
		return (ALTERED);

	case NOFIX:
	case IGNORE:
		return (0);

	default:
		errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
	}
	/* NOTREACHED */
	return (0);
}

#include <stdarg.h>

/*
 * Print details about a buffer.
 */
static void
prtbuf(const char *msg, struct bufarea *bp)
{
	
	printf("%s: bp %p, type %s, bno %jd, size %d, refcnt %d, flags %s, "
	    "index %jd\n", msg, bp, BT_BUFTYPE(bp->b_type), (intmax_t) bp->b_bno,
	    bp->b_size, bp->b_refcnt, bp->b_flags & B_DIRTY ? "dirty" : "clean",
	    (intmax_t) bp->b_index);
}

/*
 * An unexpected inconsistency occurred.
 * Die if preening or file system is running with soft dependency protocol,
 * otherwise just print message and continue.
 */
void
pfatal(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	if (!preen) {
		(void)vfprintf(stdout, fmt, ap);
		va_end(ap);
		if (usedsoftdep)
			(void)fprintf(stdout,
			    "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
		/*
		 * Force foreground fsck to clean up inconsistency.
		 */
		if (bkgrdflag) {
			cmd.value = FS_NEEDSFSCK;
			cmd.size = 1;
			if (sysctlbyname("vfs.ffs.setflags", 0, 0,
			    &cmd, sizeof cmd) == -1)
				pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
			fprintf(stdout, "CANNOT RUN IN BACKGROUND\n");
			ckfini(0);
			exit(EEXIT);
		}
		return;
	}
	if (cdevname == NULL)
		cdevname = strdup("fsck");
	(void)fprintf(stdout, "%s: ", cdevname);
	(void)vfprintf(stdout, fmt, ap);
	(void)fprintf(stdout,
	    "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
	    cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
	/*
	 * Force foreground fsck to clean up inconsistency.
	 */
	if (bkgrdflag) {
		cmd.value = FS_NEEDSFSCK;
		cmd.size = 1;
		if (sysctlbyname("vfs.ffs.setflags", 0, 0,
		    &cmd, sizeof cmd) == -1)
			pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
	}
	ckfini(0);
	exit(EEXIT);
}

/*
 * Pwarn just prints a message when not preening or running soft dependency
 * protocol, or a warning (preceded by filename) when preening.
 */
void
pwarn(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	if (preen)
		(void)fprintf(stdout, "%s: ", cdevname);
	(void)vfprintf(stdout, fmt, ap);
	va_end(ap);
}

/*
 * Stub for routines from kernel.
 */
void
panic(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	pfatal("INTERNAL INCONSISTENCY:");
	(void)vfprintf(stdout, fmt, ap);
	va_end(ap);
	exit(EEXIT);
}