1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1980, 1986, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#if 0
33#ifndef lint
34static const char sccsid[] = "@(#)utilities.c	8.6 (Berkeley) 5/19/95";
35#endif /* not lint */
36#endif
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD$");
39
40#include <sys/param.h>
41#include <sys/time.h>
42#include <sys/types.h>
43#include <sys/sysctl.h>
44#include <sys/disk.h>
45#include <sys/disklabel.h>
46#include <sys/ioctl.h>
47#include <sys/stat.h>
48
49#include <ufs/ufs/dinode.h>
50#include <ufs/ufs/dir.h>
51#include <ufs/ffs/fs.h>
52
53#include <err.h>
54#include <errno.h>
55#include <string.h>
56#include <ctype.h>
57#include <fstab.h>
58#include <stdint.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <time.h>
62#include <unistd.h>
63#include <libufs.h>
64
65#include "fsck.h"
66
67static void slowio_start(void);
68static void slowio_end(void);
69static void printIOstats(void);
70
71static long diskreads, totaldiskreads, totalreads; /* Disk cache statistics */
72static struct timespec startpass, finishpass;
73struct timeval slowio_starttime;
74int slowio_delay_usec = 10000;	/* Initial IO delay for background fsck */
75int slowio_pollcnt;
76static struct bufarea cgblk;	/* backup buffer for cylinder group blocks */
77static TAILQ_HEAD(buflist, bufarea) bufhead;	/* head of buffer cache list */
78static int numbufs;				/* size of buffer cache */
79static char *buftype[BT_NUMBUFTYPES] = BT_NAMES;
80static struct bufarea *cgbufs;	/* header for cylinder group cache */
81static int flushtries;		/* number of tries to reclaim memory */
82
83void
84fsutilinit(void)
85{
86	diskreads = totaldiskreads = totalreads = 0;
87	bzero(&startpass, sizeof(struct timespec));
88	bzero(&finishpass, sizeof(struct timespec));
89	bzero(&slowio_starttime, sizeof(struct timeval));
90	slowio_delay_usec = 10000;
91	slowio_pollcnt = 0;
92	bzero(&cgblk, sizeof(struct bufarea));
93	TAILQ_INIT(&bufhead);
94	numbufs = 0;
95	/* buftype ? */
96	cgbufs = NULL;
97	flushtries = 0;
98}
99
100int
101ftypeok(union dinode *dp)
102{
103	switch (DIP(dp, di_mode) & IFMT) {
104
105	case IFDIR:
106	case IFREG:
107	case IFBLK:
108	case IFCHR:
109	case IFLNK:
110	case IFSOCK:
111	case IFIFO:
112		return (1);
113
114	default:
115		if (debug)
116			printf("bad file type 0%o\n", DIP(dp, di_mode));
117		return (0);
118	}
119}
120
121int
122reply(const char *question)
123{
124	int persevere;
125	char c;
126
127	if (preen)
128		pfatal("INTERNAL ERROR: GOT TO reply()");
129	persevere = !strcmp(question, "CONTINUE");
130	printf("\n");
131	if (!persevere && (nflag || (fswritefd < 0 && bkgrdflag == 0))) {
132		printf("%s? no\n\n", question);
133		resolved = 0;
134		return (0);
135	}
136	if (yflag || (persevere && nflag)) {
137		printf("%s? yes\n\n", question);
138		return (1);
139	}
140	do	{
141		printf("%s? [yn] ", question);
142		(void) fflush(stdout);
143		c = getc(stdin);
144		while (c != '\n' && getc(stdin) != '\n') {
145			if (feof(stdin)) {
146				resolved = 0;
147				return (0);
148			}
149		}
150	} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
151	printf("\n");
152	if (c == 'y' || c == 'Y')
153		return (1);
154	resolved = 0;
155	return (0);
156}
157
158/*
159 * Look up state information for an inode.
160 */
161struct inostat *
162inoinfo(ino_t inum)
163{
164	static struct inostat unallocated = { USTATE, 0, 0 };
165	struct inostatlist *ilp;
166	int iloff;
167
168	if (inum > maxino)
169		errx(EEXIT, "inoinfo: inumber %ju out of range",
170		    (uintmax_t)inum);
171	ilp = &inostathead[inum / sblock.fs_ipg];
172	iloff = inum % sblock.fs_ipg;
173	if (iloff >= ilp->il_numalloced)
174		return (&unallocated);
175	return (&ilp->il_stat[iloff]);
176}
177
178/*
179 * Malloc buffers and set up cache.
180 */
181void
182bufinit(void)
183{
184	struct bufarea *bp;
185	long bufcnt, i;
186	char *bufp;
187
188	pbp = pdirbp = (struct bufarea *)0;
189	bufp = Malloc((unsigned int)sblock.fs_bsize);
190	if (bufp == NULL)
191		errx(EEXIT, "cannot allocate buffer pool");
192	cgblk.b_un.b_buf = bufp;
193	initbarea(&cgblk, BT_CYLGRP);
194	TAILQ_INIT(&bufhead);
195	bufcnt = MAXBUFS;
196	if (bufcnt < MINBUFS)
197		bufcnt = MINBUFS;
198	for (i = 0; i < bufcnt; i++) {
199		bp = (struct bufarea *)Malloc(sizeof(struct bufarea));
200		bufp = Malloc((unsigned int)sblock.fs_bsize);
201		if (bp == NULL || bufp == NULL) {
202			if (i >= MINBUFS)
203				break;
204			errx(EEXIT, "cannot allocate buffer pool");
205		}
206		bp->b_un.b_buf = bufp;
207		TAILQ_INSERT_HEAD(&bufhead, bp, b_list);
208		initbarea(bp, BT_UNKNOWN);
209	}
210	numbufs = i;	/* save number of buffers */
211	for (i = 0; i < BT_NUMBUFTYPES; i++) {
212		readtime[i].tv_sec = totalreadtime[i].tv_sec = 0;
213		readtime[i].tv_nsec = totalreadtime[i].tv_nsec = 0;
214		readcnt[i] = totalreadcnt[i] = 0;
215	}
216}
217
218/*
219 * Manage cylinder group buffers.
220 */
221static struct bufarea *cgbufs;	/* header for cylinder group cache */
222static int flushtries;		/* number of tries to reclaim memory */
223
224struct bufarea *
225cglookup(int cg)
226{
227	struct bufarea *cgbp;
228	struct cg *cgp;
229
230	if (cgbufs == NULL) {
231		cgbufs = calloc(sblock.fs_ncg, sizeof(struct bufarea));
232		if (cgbufs == NULL)
233			errx(EEXIT, "cannot allocate cylinder group buffers");
234	}
235	cgbp = &cgbufs[cg];
236	if (cgbp->b_un.b_cg != NULL)
237		return (cgbp);
238	cgp = NULL;
239	if (flushtries == 0)
240		cgp = malloc((unsigned int)sblock.fs_cgsize);
241	if (cgp == NULL) {
242		getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
243		return (&cgblk);
244	}
245	cgbp->b_un.b_cg = cgp;
246	initbarea(cgbp, BT_CYLGRP);
247	getblk(cgbp, cgtod(&sblock, cg), sblock.fs_cgsize);
248	return (cgbp);
249}
250
251/*
252 * Attempt to flush a cylinder group cache entry.
253 * Return whether the flush was successful.
254 */
255int
256flushentry(void)
257{
258	struct bufarea *cgbp;
259
260	if (flushtries == sblock.fs_ncg || cgbufs == NULL)
261		return (0);
262	cgbp = &cgbufs[flushtries++];
263	if (cgbp->b_un.b_cg == NULL)
264		return (0);
265	flush(fswritefd, cgbp);
266	free(cgbp->b_un.b_buf);
267	cgbp->b_un.b_buf = NULL;
268	return (1);
269}
270
271/*
272 * Manage a cache of directory blocks.
273 */
274struct bufarea *
275getdatablk(ufs2_daddr_t blkno, long size, int type)
276{
277	struct bufarea *bp;
278
279	TAILQ_FOREACH(bp, &bufhead, b_list)
280		if (bp->b_bno == fsbtodb(&sblock, blkno))
281			goto foundit;
282	TAILQ_FOREACH_REVERSE(bp, &bufhead, buflist, b_list)
283		if ((bp->b_flags & B_INUSE) == 0)
284			break;
285	if (bp == NULL)
286		errx(EEXIT, "deadlocked buffer pool");
287	bp->b_type = type;
288	getblk(bp, blkno, size);
289	/* fall through */
290foundit:
291	if (debug && bp->b_type != type)
292		printf("Buffer type changed from %s to %s\n",
293		    buftype[bp->b_type], buftype[type]);
294	TAILQ_REMOVE(&bufhead, bp, b_list);
295	TAILQ_INSERT_HEAD(&bufhead, bp, b_list);
296	bp->b_flags |= B_INUSE;
297	return (bp);
298}
299
300void
301getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
302{
303	ufs2_daddr_t dblk;
304	struct timespec start, finish;
305
306	dblk = fsbtodb(&sblock, blk);
307	if (bp->b_bno == dblk) {
308		totalreads++;
309	} else {
310		flush(fswritefd, bp);
311		if (debug) {
312			readcnt[bp->b_type]++;
313			clock_gettime(CLOCK_REALTIME_PRECISE, &start);
314		}
315		bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, dblk, size);
316		if (debug) {
317			clock_gettime(CLOCK_REALTIME_PRECISE, &finish);
318			timespecsub(&finish, &start, &finish);
319			timespecadd(&readtime[bp->b_type], &finish,
320			    &readtime[bp->b_type]);
321		}
322		bp->b_bno = dblk;
323		bp->b_size = size;
324	}
325}
326
327void
328flush(int fd, struct bufarea *bp)
329{
330
331	if (!bp->b_dirty)
332		return;
333	bp->b_dirty = 0;
334	if (fswritefd < 0) {
335		pfatal("WRITING IN READ_ONLY MODE.\n");
336		return;
337	}
338	if (bp->b_errs != 0)
339		pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
340		    (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
341		    (long long)bp->b_bno);
342	bp->b_errs = 0;
343	/*
344	 * Write using the appropriate function.
345	 */
346	switch (bp->b_type) {
347	case BT_SUPERBLK:
348		if (bp != &sblk)
349			pfatal("BUFFER %p DOES NOT MATCH SBLK %p\n",
350			    bp, &sblk);
351		if (sbput(fd, (struct fs *)bp->b_un.b_buf, 0) == 0)
352			fsmodified = 1;
353		break;
354	case BT_CYLGRP:
355		if (cgput(&disk, (struct cg *)bp->b_un.b_buf) == 0)
356			fsmodified = 1;
357		break;
358	default:
359		blwrite(fd, bp->b_un.b_buf, bp->b_bno, bp->b_size);
360		break;
361	}
362}
363
364void
365rwerror(const char *mesg, ufs2_daddr_t blk)
366{
367
368	if (bkgrdcheck)
369		exit(EEXIT);
370	if (preen == 0)
371		printf("\n");
372	pfatal("CANNOT %s: %ld", mesg, (long)blk);
373	if (reply("CONTINUE") == 0)
374		exit(EEXIT);
375}
376
377void
378ckfini(int markclean)
379{
380	struct bufarea *bp, *nbp;
381	int ofsmodified, cnt;
382
383	if (bkgrdflag) {
384		unlink(snapname);
385		if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) {
386			cmd.value = FS_UNCLEAN;
387			cmd.size = markclean ? -1 : 1;
388			if (sysctlbyname("vfs.ffs.setflags", 0, 0,
389			    &cmd, sizeof cmd) == -1)
390				rwerror("SET FILE SYSTEM FLAGS", FS_UNCLEAN);
391			if (!preen) {
392				printf("\n***** FILE SYSTEM MARKED %s *****\n",
393				    markclean ? "CLEAN" : "DIRTY");
394				if (!markclean)
395					rerun = 1;
396			}
397		} else if (!preen && !markclean) {
398			printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
399			rerun = 1;
400		}
401	}
402	if (debug && totalreads > 0)
403		printf("cache with %d buffers missed %ld of %ld (%d%%)\n",
404		    numbufs, totaldiskreads, totalreads,
405		    (int)(totaldiskreads * 100 / totalreads));
406	if (fswritefd < 0) {
407		(void)close(fsreadfd);
408		return;
409	}
410	flush(fswritefd, &sblk);
411	if (havesb && cursnapshot == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
412	    sblk.b_bno != sblock.fs_sblockloc / dev_bsize &&
413	    !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
414		/* Change the write destination to standard superblock */
415		sblock.fs_sblockactualloc = sblock.fs_sblockloc;
416		sblk.b_bno = sblock.fs_sblockloc / dev_bsize;
417		sbdirty();
418		flush(fswritefd, &sblk);
419	}
420	flush(fswritefd, &cgblk);
421	free(cgblk.b_un.b_buf);
422	cnt = 0;
423	TAILQ_FOREACH_REVERSE_SAFE(bp, &bufhead, buflist, b_list, nbp) {
424		TAILQ_REMOVE(&bufhead, bp, b_list);
425		cnt++;
426		flush(fswritefd, bp);
427		free(bp->b_un.b_buf);
428		free((char *)bp);
429	}
430	if (numbufs != cnt)
431		errx(EEXIT, "panic: lost %d buffers", numbufs - cnt);
432	if (cgbufs != NULL) {
433		for (cnt = 0; cnt < sblock.fs_ncg; cnt++) {
434			if (cgbufs[cnt].b_un.b_cg == NULL)
435				continue;
436			flush(fswritefd, &cgbufs[cnt]);
437			free(cgbufs[cnt].b_un.b_cg);
438		}
439		free(cgbufs);
440	}
441	pbp = pdirbp = (struct bufarea *)0;
442	if (cursnapshot == 0 && sblock.fs_clean != markclean) {
443		if ((sblock.fs_clean = markclean) != 0) {
444			sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK);
445			sblock.fs_pendingblocks = 0;
446			sblock.fs_pendinginodes = 0;
447		}
448		sbdirty();
449		ofsmodified = fsmodified;
450		flush(fswritefd, &sblk);
451		fsmodified = ofsmodified;
452		if (!preen) {
453			printf("\n***** FILE SYSTEM MARKED %s *****\n",
454			    markclean ? "CLEAN" : "DIRTY");
455			if (!markclean)
456				rerun = 1;
457		}
458	} else if (!preen) {
459		if (markclean) {
460			printf("\n***** FILE SYSTEM IS CLEAN *****\n");
461		} else {
462			printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
463			rerun = 1;
464		}
465	}
466	(void)close(fsreadfd);
467	(void)close(fswritefd);
468}
469
470/*
471 * Print out I/O statistics.
472 */
473void
474IOstats(char *what)
475{
476	int i;
477
478	if (debug == 0)
479		return;
480	if (diskreads == 0) {
481		printf("%s: no I/O\n\n", what);
482		return;
483	}
484	if (startpass.tv_sec == 0)
485		startpass = startprog;
486	printf("%s: I/O statistics\n", what);
487	printIOstats();
488	totaldiskreads += diskreads;
489	diskreads = 0;
490	for (i = 0; i < BT_NUMBUFTYPES; i++) {
491		timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
492		totalreadcnt[i] += readcnt[i];
493		readtime[i].tv_sec = readtime[i].tv_nsec = 0;
494		readcnt[i] = 0;
495	}
496	clock_gettime(CLOCK_REALTIME_PRECISE, &startpass);
497}
498
499void
500finalIOstats(void)
501{
502	int i;
503
504	if (debug == 0)
505		return;
506	printf("Final I/O statistics\n");
507	totaldiskreads += diskreads;
508	diskreads = totaldiskreads;
509	startpass = startprog;
510	for (i = 0; i < BT_NUMBUFTYPES; i++) {
511		timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
512		totalreadcnt[i] += readcnt[i];
513		readtime[i] = totalreadtime[i];
514		readcnt[i] = totalreadcnt[i];
515	}
516	printIOstats();
517}
518
519static void printIOstats(void)
520{
521	long long msec, totalmsec;
522	int i;
523
524	clock_gettime(CLOCK_REALTIME_PRECISE, &finishpass);
525	timespecsub(&finishpass, &startpass, &finishpass);
526	printf("Running time: %jd.%03ld sec\n",
527		(intmax_t)finishpass.tv_sec, finishpass.tv_nsec / 1000000);
528	printf("buffer reads by type:\n");
529	for (totalmsec = 0, i = 0; i < BT_NUMBUFTYPES; i++)
530		totalmsec += readtime[i].tv_sec * 1000 +
531		    readtime[i].tv_nsec / 1000000;
532	if (totalmsec == 0)
533		totalmsec = 1;
534	for (i = 0; i < BT_NUMBUFTYPES; i++) {
535		if (readcnt[i] == 0)
536			continue;
537		msec =
538		    readtime[i].tv_sec * 1000 + readtime[i].tv_nsec / 1000000;
539		printf("%21s:%8ld %2ld.%ld%% %4jd.%03ld sec %2lld.%lld%%\n",
540		    buftype[i], readcnt[i], readcnt[i] * 100 / diskreads,
541		    (readcnt[i] * 1000 / diskreads) % 10,
542		    (intmax_t)readtime[i].tv_sec, readtime[i].tv_nsec / 1000000,
543		    msec * 100 / totalmsec, (msec * 1000 / totalmsec) % 10);
544	}
545	printf("\n");
546}
547
548int
549blread(int fd, char *buf, ufs2_daddr_t blk, long size)
550{
551	char *cp;
552	int i, errs;
553	off_t offset;
554
555	offset = blk;
556	offset *= dev_bsize;
557	if (bkgrdflag)
558		slowio_start();
559	totalreads++;
560	diskreads++;
561	if (lseek(fd, offset, 0) < 0)
562		rwerror("SEEK BLK", blk);
563	else if (read(fd, buf, (int)size) == size) {
564		if (bkgrdflag)
565			slowio_end();
566		return (0);
567	}
568
569	/*
570	 * This is handled specially here instead of in rwerror because
571	 * rwerror is used for all sorts of errors, not just true read/write
572	 * errors.  It should be refactored and fixed.
573	 */
574	if (surrender) {
575		pfatal("CANNOT READ_BLK: %ld", (long)blk);
576		errx(EEXIT, "ABORTING DUE TO READ ERRORS");
577	} else
578		rwerror("READ BLK", blk);
579
580	if (lseek(fd, offset, 0) < 0)
581		rwerror("SEEK BLK", blk);
582	errs = 0;
583	memset(buf, 0, (size_t)size);
584	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
585	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
586		if (read(fd, cp, (int)secsize) != secsize) {
587			(void)lseek(fd, offset + i + secsize, 0);
588			if (secsize != dev_bsize && dev_bsize != 1)
589				printf(" %jd (%jd),",
590				    (intmax_t)(blk * dev_bsize + i) / secsize,
591				    (intmax_t)blk + i / dev_bsize);
592			else
593				printf(" %jd,", (intmax_t)blk + i / dev_bsize);
594			errs++;
595		}
596	}
597	printf("\n");
598	if (errs)
599		resolved = 0;
600	return (errs);
601}
602
603void
604blwrite(int fd, char *buf, ufs2_daddr_t blk, ssize_t size)
605{
606	int i;
607	char *cp;
608	off_t offset;
609
610	if (fd < 0)
611		return;
612	offset = blk;
613	offset *= dev_bsize;
614	if (lseek(fd, offset, 0) < 0)
615		rwerror("SEEK BLK", blk);
616	else if (write(fd, buf, size) == size) {
617		fsmodified = 1;
618		return;
619	}
620	resolved = 0;
621	rwerror("WRITE BLK", blk);
622	if (lseek(fd, offset, 0) < 0)
623		rwerror("SEEK BLK", blk);
624	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
625	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
626		if (write(fd, cp, dev_bsize) != dev_bsize) {
627			(void)lseek(fd, offset + i + dev_bsize, 0);
628			printf(" %jd,", (intmax_t)blk + i / dev_bsize);
629		}
630	printf("\n");
631	return;
632}
633
634void
635blerase(int fd, ufs2_daddr_t blk, long size)
636{
637	off_t ioarg[2];
638
639	if (fd < 0)
640		return;
641	ioarg[0] = blk * dev_bsize;
642	ioarg[1] = size;
643	ioctl(fd, DIOCGDELETE, ioarg);
644	/* we don't really care if we succeed or not */
645	return;
646}
647
648/*
649 * Fill a contiguous region with all-zeroes.  Note ZEROBUFSIZE is by
650 * definition a multiple of dev_bsize.
651 */
652void
653blzero(int fd, ufs2_daddr_t blk, long size)
654{
655	static char *zero;
656	off_t offset, len;
657
658	if (fd < 0)
659		return;
660	if (zero == NULL) {
661		zero = calloc(ZEROBUFSIZE, 1);
662		if (zero == NULL)
663			errx(EEXIT, "cannot allocate buffer pool");
664	}
665	offset = blk * dev_bsize;
666	if (lseek(fd, offset, 0) < 0)
667		rwerror("SEEK BLK", blk);
668	while (size > 0) {
669		len = MIN(ZEROBUFSIZE, size);
670		if (write(fd, zero, len) != len)
671			rwerror("WRITE BLK", blk);
672		blk += len / dev_bsize;
673		size -= len;
674	}
675}
676
677/*
678 * Verify cylinder group's magic number and other parameters.  If the
679 * test fails, offer an option to rebuild the whole cylinder group.
680 */
681int
682check_cgmagic(int cg, struct bufarea *cgbp)
683{
684	struct cg *cgp = cgbp->b_un.b_cg;
685	static int prevfailcg = -1;
686
687	/*
688	 * Extended cylinder group checks.
689	 */
690	if (cg_chkmagic(cgp) &&
691	    ((sblock.fs_magic == FS_UFS1_MAGIC &&
692	      cgp->cg_old_niblk == sblock.fs_ipg &&
693	      cgp->cg_ndblk <= sblock.fs_fpg &&
694	      cgp->cg_old_ncyl <= sblock.fs_old_cpg) ||
695	     (sblock.fs_magic == FS_UFS2_MAGIC &&
696	      cgp->cg_niblk == sblock.fs_ipg &&
697	      cgp->cg_ndblk <= sblock.fs_fpg &&
698	      cgp->cg_initediblk <= sblock.fs_ipg))) {
699		return (1);
700	}
701	if (prevfailcg == cg)
702		return (0);
703	prevfailcg = cg;
704	pfatal("CYLINDER GROUP %d: BAD MAGIC NUMBER", cg);
705	if (!reply("REBUILD CYLINDER GROUP")) {
706		printf("YOU WILL NEED TO RERUN FSCK.\n");
707		rerun = 1;
708		return (1);
709	}
710	/*
711	 * Zero out the cylinder group and then initialize critical fields.
712	 * Bit maps and summaries will be recalculated by later passes.
713	 */
714	memset(cgp, 0, (size_t)sblock.fs_cgsize);
715	cgp->cg_magic = CG_MAGIC;
716	cgp->cg_cgx = cg;
717	cgp->cg_niblk = sblock.fs_ipg;
718	cgp->cg_initediblk = MIN(sblock.fs_ipg, 2 * INOPB(&sblock));
719	if (cgbase(&sblock, cg) + sblock.fs_fpg < sblock.fs_size)
720		cgp->cg_ndblk = sblock.fs_fpg;
721	else
722		cgp->cg_ndblk = sblock.fs_size - cgbase(&sblock, cg);
723	cgp->cg_iusedoff = &cgp->cg_space[0] - (u_char *)(&cgp->cg_firstfield);
724	if (sblock.fs_magic == FS_UFS1_MAGIC) {
725		cgp->cg_niblk = 0;
726		cgp->cg_initediblk = 0;
727		cgp->cg_old_ncyl = sblock.fs_old_cpg;
728		cgp->cg_old_niblk = sblock.fs_ipg;
729		cgp->cg_old_btotoff = cgp->cg_iusedoff;
730		cgp->cg_old_boff = cgp->cg_old_btotoff +
731		    sblock.fs_old_cpg * sizeof(int32_t);
732		cgp->cg_iusedoff = cgp->cg_old_boff +
733		    sblock.fs_old_cpg * sizeof(u_int16_t);
734	}
735	cgp->cg_freeoff = cgp->cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
736	cgp->cg_nextfreeoff = cgp->cg_freeoff + howmany(sblock.fs_fpg,CHAR_BIT);
737	if (sblock.fs_contigsumsize > 0) {
738		cgp->cg_nclusterblks = cgp->cg_ndblk / sblock.fs_frag;
739		cgp->cg_clustersumoff =
740		    roundup(cgp->cg_nextfreeoff, sizeof(u_int32_t));
741		cgp->cg_clustersumoff -= sizeof(u_int32_t);
742		cgp->cg_clusteroff = cgp->cg_clustersumoff +
743		    (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
744		cgp->cg_nextfreeoff = cgp->cg_clusteroff +
745		    howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
746	}
747	dirty(cgbp);
748	return (0);
749}
750
751/*
752 * allocate a data block with the specified number of fragments
753 */
754ufs2_daddr_t
755allocblk(long frags)
756{
757	int i, j, k, cg, baseblk;
758	struct bufarea *cgbp;
759	struct cg *cgp;
760
761	if (frags <= 0 || frags > sblock.fs_frag)
762		return (0);
763	for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
764		for (j = 0; j <= sblock.fs_frag - frags; j++) {
765			if (testbmap(i + j))
766				continue;
767			for (k = 1; k < frags; k++)
768				if (testbmap(i + j + k))
769					break;
770			if (k < frags) {
771				j += k;
772				continue;
773			}
774			cg = dtog(&sblock, i + j);
775			cgbp = cglookup(cg);
776			cgp = cgbp->b_un.b_cg;
777			if (!check_cgmagic(cg, cgbp)) {
778				i = (cg + 1) * sblock.fs_fpg - sblock.fs_frag;
779				continue;
780			}
781			baseblk = dtogd(&sblock, i + j);
782			for (k = 0; k < frags; k++) {
783				setbmap(i + j + k);
784				clrbit(cg_blksfree(cgp), baseblk + k);
785			}
786			n_blks += frags;
787			if (frags == sblock.fs_frag)
788				cgp->cg_cs.cs_nbfree--;
789			else
790				cgp->cg_cs.cs_nffree -= frags;
791			dirty(cgbp);
792			return (i + j);
793		}
794	}
795	return (0);
796}
797
798/*
799 * Free a previously allocated block
800 */
801void
802freeblk(ufs2_daddr_t blkno, long frags)
803{
804	struct inodesc idesc;
805
806	idesc.id_blkno = blkno;
807	idesc.id_numfrags = frags;
808	(void)pass4check(&idesc);
809}
810
811/* Slow down IO so as to leave some disk bandwidth for other processes */
812void
813slowio_start()
814{
815
816	/* Delay one in every 8 operations */
817	slowio_pollcnt = (slowio_pollcnt + 1) & 7;
818	if (slowio_pollcnt == 0) {
819		gettimeofday(&slowio_starttime, NULL);
820	}
821}
822
823void
824slowio_end()
825{
826	struct timeval tv;
827	int delay_usec;
828
829	if (slowio_pollcnt != 0)
830		return;
831
832	/* Update the slowdown interval. */
833	gettimeofday(&tv, NULL);
834	delay_usec = (tv.tv_sec - slowio_starttime.tv_sec) * 1000000 +
835	    (tv.tv_usec - slowio_starttime.tv_usec);
836	if (delay_usec < 64)
837		delay_usec = 64;
838	if (delay_usec > 2500000)
839		delay_usec = 2500000;
840	slowio_delay_usec = (slowio_delay_usec * 63 + delay_usec) >> 6;
841	/* delay by 8 times the average IO delay */
842	if (slowio_delay_usec > 64)
843		usleep(slowio_delay_usec * 8);
844}
845
846/*
847 * Find a pathname
848 */
849void
850getpathname(char *namebuf, ino_t curdir, ino_t ino)
851{
852	int len;
853	char *cp;
854	struct inodesc idesc;
855	static int busy = 0;
856
857	if (curdir == ino && ino == UFS_ROOTINO) {
858		(void)strcpy(namebuf, "/");
859		return;
860	}
861	if (busy || !INO_IS_DVALID(curdir)) {
862		(void)strcpy(namebuf, "?");
863		return;
864	}
865	busy = 1;
866	memset(&idesc, 0, sizeof(struct inodesc));
867	idesc.id_type = DATA;
868	idesc.id_fix = IGNORE;
869	cp = &namebuf[MAXPATHLEN - 1];
870	*cp = '\0';
871	if (curdir != ino) {
872		idesc.id_parent = curdir;
873		goto namelookup;
874	}
875	while (ino != UFS_ROOTINO) {
876		idesc.id_number = ino;
877		idesc.id_func = findino;
878		idesc.id_name = strdup("..");
879		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
880			break;
881	namelookup:
882		idesc.id_number = idesc.id_parent;
883		idesc.id_parent = ino;
884		idesc.id_func = findname;
885		idesc.id_name = namebuf;
886		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
887			break;
888		len = strlen(namebuf);
889		cp -= len;
890		memmove(cp, namebuf, (size_t)len);
891		*--cp = '/';
892		if (cp < &namebuf[UFS_MAXNAMLEN])
893			break;
894		ino = idesc.id_number;
895	}
896	busy = 0;
897	if (ino != UFS_ROOTINO)
898		*--cp = '?';
899	memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
900}
901
902void
903catch(int sig __unused)
904{
905
906	ckfini(0);
907	exit(12);
908}
909
910/*
911 * When preening, allow a single quit to signal
912 * a special exit after file system checks complete
913 * so that reboot sequence may be interrupted.
914 */
915void
916catchquit(int sig __unused)
917{
918	printf("returning to single-user after file system check\n");
919	returntosingle = 1;
920	(void)signal(SIGQUIT, SIG_DFL);
921}
922
923/*
924 * determine whether an inode should be fixed.
925 */
926int
927dofix(struct inodesc *idesc, const char *msg)
928{
929
930	switch (idesc->id_fix) {
931
932	case DONTKNOW:
933		if (idesc->id_type == DATA)
934			direrror(idesc->id_number, msg);
935		else
936			pwarn("%s", msg);
937		if (preen) {
938			printf(" (SALVAGED)\n");
939			idesc->id_fix = FIX;
940			return (ALTERED);
941		}
942		if (reply("SALVAGE") == 0) {
943			idesc->id_fix = NOFIX;
944			return (0);
945		}
946		idesc->id_fix = FIX;
947		return (ALTERED);
948
949	case FIX:
950		return (ALTERED);
951
952	case NOFIX:
953	case IGNORE:
954		return (0);
955
956	default:
957		errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
958	}
959	/* NOTREACHED */
960	return (0);
961}
962
963#include <stdarg.h>
964
965/*
966 * An unexpected inconsistency occurred.
967 * Die if preening or file system is running with soft dependency protocol,
968 * otherwise just print message and continue.
969 */
970void
971pfatal(const char *fmt, ...)
972{
973	va_list ap;
974	va_start(ap, fmt);
975	if (!preen) {
976		(void)vfprintf(stdout, fmt, ap);
977		va_end(ap);
978		if (usedsoftdep)
979			(void)fprintf(stdout,
980			    "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
981		/*
982		 * Force foreground fsck to clean up inconsistency.
983		 */
984		if (bkgrdflag) {
985			cmd.value = FS_NEEDSFSCK;
986			cmd.size = 1;
987			if (sysctlbyname("vfs.ffs.setflags", 0, 0,
988			    &cmd, sizeof cmd) == -1)
989				pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
990			fprintf(stdout, "CANNOT RUN IN BACKGROUND\n");
991			ckfini(0);
992			exit(EEXIT);
993		}
994		return;
995	}
996	if (cdevname == NULL)
997		cdevname = strdup("fsck");
998	(void)fprintf(stdout, "%s: ", cdevname);
999	(void)vfprintf(stdout, fmt, ap);
1000	(void)fprintf(stdout,
1001	    "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
1002	    cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
1003	/*
1004	 * Force foreground fsck to clean up inconsistency.
1005	 */
1006	if (bkgrdflag) {
1007		cmd.value = FS_NEEDSFSCK;
1008		cmd.size = 1;
1009		if (sysctlbyname("vfs.ffs.setflags", 0, 0,
1010		    &cmd, sizeof cmd) == -1)
1011			pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
1012	}
1013	ckfini(0);
1014	exit(EEXIT);
1015}
1016
1017/*
1018 * Pwarn just prints a message when not preening or running soft dependency
1019 * protocol, or a warning (preceded by filename) when preening.
1020 */
1021void
1022pwarn(const char *fmt, ...)
1023{
1024	va_list ap;
1025	va_start(ap, fmt);
1026	if (preen)
1027		(void)fprintf(stdout, "%s: ", cdevname);
1028	(void)vfprintf(stdout, fmt, ap);
1029	va_end(ap);
1030}
1031
1032/*
1033 * Stub for routines from kernel.
1034 */
1035void
1036panic(const char *fmt, ...)
1037{
1038	va_list ap;
1039	va_start(ap, fmt);
1040	pfatal("INTERNAL INCONSISTENCY:");
1041	(void)vfprintf(stdout, fmt, ap);
1042	va_end(ap);
1043	exit(EEXIT);
1044}
1045