xref: /freebsd/sbin/fsck_ffs/fsutil.c (revision 7cc42f6d25ef2e19059d088fa7d4853fe9afefb5)
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
34 static 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 
67 static void slowio_start(void);
68 static void slowio_end(void);
69 static void printIOstats(void);
70 
71 static long diskreads, totaldiskreads, totalreads; /* Disk cache statistics */
72 static struct timespec startpass, finishpass;
73 struct timeval slowio_starttime;
74 int slowio_delay_usec = 10000;	/* Initial IO delay for background fsck */
75 int slowio_pollcnt;
76 static struct bufarea cgblk;	/* backup buffer for cylinder group blocks */
77 static TAILQ_HEAD(buflist, bufarea) bufhead;	/* head of buffer cache list */
78 static int numbufs;				/* size of buffer cache */
79 static char *buftype[BT_NUMBUFTYPES] = BT_NAMES;
80 static struct bufarea *cgbufs;	/* header for cylinder group cache */
81 static int flushtries;		/* number of tries to reclaim memory */
82 
83 void
84 fsutilinit(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 
100 int
101 ftypeok(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 
121 int
122 reply(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  */
161 struct inostat *
162 inoinfo(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  */
181 void
182 bufinit(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  *
221  * Use getblk() here rather than cgget() because the cylinder group
222  * may be corrupted but we want it anyway so we can fix it.
223  */
224 static struct bufarea *cgbufs;	/* header for cylinder group cache */
225 static int flushtries;		/* number of tries to reclaim memory */
226 
227 struct bufarea *
228 cglookup(int cg)
229 {
230 	struct bufarea *cgbp;
231 	struct cg *cgp;
232 
233 	if (cgbufs == NULL) {
234 		cgbufs = calloc(sblock.fs_ncg, sizeof(struct bufarea));
235 		if (cgbufs == NULL)
236 			errx(EEXIT, "cannot allocate cylinder group buffers");
237 	}
238 	cgbp = &cgbufs[cg];
239 	if (cgbp->b_un.b_cg != NULL)
240 		return (cgbp);
241 	cgp = NULL;
242 	if (flushtries == 0)
243 		cgp = malloc((unsigned int)sblock.fs_cgsize);
244 	if (cgp == NULL) {
245 		getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
246 		return (&cgblk);
247 	}
248 	cgbp->b_un.b_cg = cgp;
249 	initbarea(cgbp, BT_CYLGRP);
250 	getblk(cgbp, cgtod(&sblock, cg), sblock.fs_cgsize);
251 	return (cgbp);
252 }
253 
254 /*
255  * Mark a cylinder group buffer as dirty.
256  * Update its check-hash if they are enabled.
257  */
258 void
259 cgdirty(struct bufarea *cgbp)
260 {
261 	struct cg *cg;
262 
263 	cg = cgbp->b_un.b_cg;
264 	if ((sblock.fs_metackhash & CK_CYLGRP) != 0) {
265 		cg->cg_ckhash = 0;
266 		cg->cg_ckhash =
267 		    calculate_crc32c(~0L, (void *)cg, sblock.fs_cgsize);
268 	}
269 	dirty(cgbp);
270 }
271 
272 /*
273  * Attempt to flush a cylinder group cache entry.
274  * Return whether the flush was successful.
275  */
276 int
277 flushentry(void)
278 {
279 	struct bufarea *cgbp;
280 
281 	if (flushtries == sblock.fs_ncg || cgbufs == NULL)
282 		return (0);
283 	cgbp = &cgbufs[flushtries++];
284 	if (cgbp->b_un.b_cg == NULL)
285 		return (0);
286 	flush(fswritefd, cgbp);
287 	free(cgbp->b_un.b_buf);
288 	cgbp->b_un.b_buf = NULL;
289 	return (1);
290 }
291 
292 /*
293  * Manage a cache of directory blocks.
294  */
295 struct bufarea *
296 getdatablk(ufs2_daddr_t blkno, long size, int type)
297 {
298 	struct bufarea *bp;
299 
300 	TAILQ_FOREACH(bp, &bufhead, b_list)
301 		if (bp->b_bno == fsbtodb(&sblock, blkno))
302 			goto foundit;
303 	TAILQ_FOREACH_REVERSE(bp, &bufhead, buflist, b_list)
304 		if ((bp->b_flags & B_INUSE) == 0)
305 			break;
306 	if (bp == NULL)
307 		errx(EEXIT, "deadlocked buffer pool");
308 	bp->b_type = type;
309 	getblk(bp, blkno, size);
310 	/* fall through */
311 foundit:
312 	if (debug && bp->b_type != type)
313 		printf("Buffer type changed from %s to %s\n",
314 		    buftype[bp->b_type], buftype[type]);
315 	TAILQ_REMOVE(&bufhead, bp, b_list);
316 	TAILQ_INSERT_HEAD(&bufhead, bp, b_list);
317 	bp->b_flags |= B_INUSE;
318 	return (bp);
319 }
320 
321 void
322 getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
323 {
324 	ufs2_daddr_t dblk;
325 	struct timespec start, finish;
326 
327 	dblk = fsbtodb(&sblock, blk);
328 	if (bp->b_bno == dblk) {
329 		totalreads++;
330 	} else {
331 		flush(fswritefd, bp);
332 		if (debug) {
333 			readcnt[bp->b_type]++;
334 			clock_gettime(CLOCK_REALTIME_PRECISE, &start);
335 		}
336 		bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, dblk, size);
337 		if (debug) {
338 			clock_gettime(CLOCK_REALTIME_PRECISE, &finish);
339 			timespecsub(&finish, &start, &finish);
340 			timespecadd(&readtime[bp->b_type], &finish,
341 			    &readtime[bp->b_type]);
342 		}
343 		bp->b_bno = dblk;
344 		bp->b_size = size;
345 	}
346 }
347 
348 void
349 flush(int fd, struct bufarea *bp)
350 {
351 
352 	if (!bp->b_dirty)
353 		return;
354 	bp->b_dirty = 0;
355 	if (fswritefd < 0) {
356 		pfatal("WRITING IN READ_ONLY MODE.\n");
357 		return;
358 	}
359 	if (bp->b_errs != 0)
360 		pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
361 		    (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
362 		    (long long)bp->b_bno);
363 	bp->b_errs = 0;
364 	/*
365 	 * Write using the appropriate function.
366 	 */
367 	switch (bp->b_type) {
368 	case BT_SUPERBLK:
369 		if (bp != &sblk)
370 			pfatal("BUFFER %p DOES NOT MATCH SBLK %p\n",
371 			    bp, &sblk);
372 		if (sbput(fd, bp->b_un.b_fs, 0) == 0)
373 			fsmodified = 1;
374 		break;
375 	case BT_CYLGRP:
376 		if (cgput(fswritefd, &sblock, bp->b_un.b_cg) == 0)
377 			fsmodified = 1;
378 		break;
379 	default:
380 		blwrite(fd, bp->b_un.b_buf, bp->b_bno, bp->b_size);
381 		break;
382 	}
383 }
384 
385 void
386 rwerror(const char *mesg, ufs2_daddr_t blk)
387 {
388 
389 	if (bkgrdcheck)
390 		exit(EEXIT);
391 	if (preen == 0)
392 		printf("\n");
393 	pfatal("CANNOT %s: %ld", mesg, (long)blk);
394 	if (reply("CONTINUE") == 0)
395 		exit(EEXIT);
396 }
397 
398 void
399 ckfini(int markclean)
400 {
401 	struct bufarea *bp, *nbp;
402 	int ofsmodified, cnt;
403 
404 	if (bkgrdflag) {
405 		unlink(snapname);
406 		if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) {
407 			cmd.value = FS_UNCLEAN;
408 			cmd.size = markclean ? -1 : 1;
409 			if (sysctlbyname("vfs.ffs.setflags", 0, 0,
410 			    &cmd, sizeof cmd) == -1)
411 				rwerror("SET FILE SYSTEM FLAGS", FS_UNCLEAN);
412 			if (!preen) {
413 				printf("\n***** FILE SYSTEM MARKED %s *****\n",
414 				    markclean ? "CLEAN" : "DIRTY");
415 				if (!markclean)
416 					rerun = 1;
417 			}
418 		} else if (!preen && !markclean) {
419 			printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
420 			rerun = 1;
421 		}
422 	}
423 	if (debug && totalreads > 0)
424 		printf("cache with %d buffers missed %ld of %ld (%d%%)\n",
425 		    numbufs, totaldiskreads, totalreads,
426 		    (int)(totaldiskreads * 100 / totalreads));
427 	if (fswritefd < 0) {
428 		(void)close(fsreadfd);
429 		return;
430 	}
431 	flush(fswritefd, &sblk);
432 	if (havesb && cursnapshot == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
433 	    sblk.b_bno != sblock.fs_sblockloc / dev_bsize &&
434 	    !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
435 		/* Change the write destination to standard superblock */
436 		sblock.fs_sblockactualloc = sblock.fs_sblockloc;
437 		sblk.b_bno = sblock.fs_sblockloc / dev_bsize;
438 		sbdirty();
439 		flush(fswritefd, &sblk);
440 	}
441 	flush(fswritefd, &cgblk);
442 	free(cgblk.b_un.b_buf);
443 	cnt = 0;
444 	TAILQ_FOREACH_REVERSE_SAFE(bp, &bufhead, buflist, b_list, nbp) {
445 		TAILQ_REMOVE(&bufhead, bp, b_list);
446 		cnt++;
447 		flush(fswritefd, bp);
448 		free(bp->b_un.b_buf);
449 		free((char *)bp);
450 	}
451 	if (numbufs != cnt)
452 		errx(EEXIT, "panic: lost %d buffers", numbufs - cnt);
453 	if (cgbufs != NULL) {
454 		for (cnt = 0; cnt < sblock.fs_ncg; cnt++) {
455 			if (cgbufs[cnt].b_un.b_cg == NULL)
456 				continue;
457 			flush(fswritefd, &cgbufs[cnt]);
458 			free(cgbufs[cnt].b_un.b_cg);
459 		}
460 		free(cgbufs);
461 	}
462 	pbp = pdirbp = (struct bufarea *)0;
463 	if (cursnapshot == 0 && sblock.fs_clean != markclean) {
464 		if ((sblock.fs_clean = markclean) != 0) {
465 			sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK);
466 			sblock.fs_pendingblocks = 0;
467 			sblock.fs_pendinginodes = 0;
468 		}
469 		sbdirty();
470 		ofsmodified = fsmodified;
471 		flush(fswritefd, &sblk);
472 		fsmodified = ofsmodified;
473 		if (!preen) {
474 			printf("\n***** FILE SYSTEM MARKED %s *****\n",
475 			    markclean ? "CLEAN" : "DIRTY");
476 			if (!markclean)
477 				rerun = 1;
478 		}
479 	} else if (!preen) {
480 		if (markclean) {
481 			printf("\n***** FILE SYSTEM IS CLEAN *****\n");
482 		} else {
483 			printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
484 			rerun = 1;
485 		}
486 	}
487 	(void)close(fsreadfd);
488 	(void)close(fswritefd);
489 }
490 
491 /*
492  * Print out I/O statistics.
493  */
494 void
495 IOstats(char *what)
496 {
497 	int i;
498 
499 	if (debug == 0)
500 		return;
501 	if (diskreads == 0) {
502 		printf("%s: no I/O\n\n", what);
503 		return;
504 	}
505 	if (startpass.tv_sec == 0)
506 		startpass = startprog;
507 	printf("%s: I/O statistics\n", what);
508 	printIOstats();
509 	totaldiskreads += diskreads;
510 	diskreads = 0;
511 	for (i = 0; i < BT_NUMBUFTYPES; i++) {
512 		timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
513 		totalreadcnt[i] += readcnt[i];
514 		readtime[i].tv_sec = readtime[i].tv_nsec = 0;
515 		readcnt[i] = 0;
516 	}
517 	clock_gettime(CLOCK_REALTIME_PRECISE, &startpass);
518 }
519 
520 void
521 finalIOstats(void)
522 {
523 	int i;
524 
525 	if (debug == 0)
526 		return;
527 	printf("Final I/O statistics\n");
528 	totaldiskreads += diskreads;
529 	diskreads = totaldiskreads;
530 	startpass = startprog;
531 	for (i = 0; i < BT_NUMBUFTYPES; i++) {
532 		timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
533 		totalreadcnt[i] += readcnt[i];
534 		readtime[i] = totalreadtime[i];
535 		readcnt[i] = totalreadcnt[i];
536 	}
537 	printIOstats();
538 }
539 
540 static void printIOstats(void)
541 {
542 	long long msec, totalmsec;
543 	int i;
544 
545 	clock_gettime(CLOCK_REALTIME_PRECISE, &finishpass);
546 	timespecsub(&finishpass, &startpass, &finishpass);
547 	printf("Running time: %jd.%03ld sec\n",
548 		(intmax_t)finishpass.tv_sec, finishpass.tv_nsec / 1000000);
549 	printf("buffer reads by type:\n");
550 	for (totalmsec = 0, i = 0; i < BT_NUMBUFTYPES; i++)
551 		totalmsec += readtime[i].tv_sec * 1000 +
552 		    readtime[i].tv_nsec / 1000000;
553 	if (totalmsec == 0)
554 		totalmsec = 1;
555 	for (i = 0; i < BT_NUMBUFTYPES; i++) {
556 		if (readcnt[i] == 0)
557 			continue;
558 		msec =
559 		    readtime[i].tv_sec * 1000 + readtime[i].tv_nsec / 1000000;
560 		printf("%21s:%8ld %2ld.%ld%% %4jd.%03ld sec %2lld.%lld%%\n",
561 		    buftype[i], readcnt[i], readcnt[i] * 100 / diskreads,
562 		    (readcnt[i] * 1000 / diskreads) % 10,
563 		    (intmax_t)readtime[i].tv_sec, readtime[i].tv_nsec / 1000000,
564 		    msec * 100 / totalmsec, (msec * 1000 / totalmsec) % 10);
565 	}
566 	printf("\n");
567 }
568 
569 int
570 blread(int fd, char *buf, ufs2_daddr_t blk, long size)
571 {
572 	char *cp;
573 	int i, errs;
574 	off_t offset;
575 
576 	offset = blk;
577 	offset *= dev_bsize;
578 	if (bkgrdflag)
579 		slowio_start();
580 	totalreads++;
581 	diskreads++;
582 	if (pread(fd, buf, (int)size, offset) == size) {
583 		if (bkgrdflag)
584 			slowio_end();
585 		return (0);
586 	}
587 
588 	/*
589 	 * This is handled specially here instead of in rwerror because
590 	 * rwerror is used for all sorts of errors, not just true read/write
591 	 * errors.  It should be refactored and fixed.
592 	 */
593 	if (surrender) {
594 		pfatal("CANNOT READ_BLK: %ld", (long)blk);
595 		errx(EEXIT, "ABORTING DUE TO READ ERRORS");
596 	} else
597 		rwerror("READ BLK", blk);
598 
599 	errs = 0;
600 	memset(buf, 0, (size_t)size);
601 	printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
602 	for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
603 		if (pread(fd, cp, (int)secsize, offset + i) != secsize) {
604 			if (secsize != dev_bsize && dev_bsize != 1)
605 				printf(" %jd (%jd),",
606 				    (intmax_t)(blk * dev_bsize + i) / secsize,
607 				    (intmax_t)blk + i / dev_bsize);
608 			else
609 				printf(" %jd,", (intmax_t)blk + i / dev_bsize);
610 			errs++;
611 		}
612 	}
613 	printf("\n");
614 	if (errs)
615 		resolved = 0;
616 	return (errs);
617 }
618 
619 void
620 blwrite(int fd, char *buf, ufs2_daddr_t blk, ssize_t size)
621 {
622 	int i;
623 	char *cp;
624 	off_t offset;
625 
626 	if (fd < 0)
627 		return;
628 	offset = blk;
629 	offset *= dev_bsize;
630 	if (pwrite(fd, buf, size, offset) == size) {
631 		fsmodified = 1;
632 		return;
633 	}
634 	resolved = 0;
635 	rwerror("WRITE BLK", blk);
636 	printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
637 	for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
638 		if (pwrite(fd, cp, dev_bsize, offset + i) != dev_bsize)
639 			printf(" %jd,", (intmax_t)blk + i / dev_bsize);
640 	printf("\n");
641 	return;
642 }
643 
644 void
645 blerase(int fd, ufs2_daddr_t blk, long size)
646 {
647 	off_t ioarg[2];
648 
649 	if (fd < 0)
650 		return;
651 	ioarg[0] = blk * dev_bsize;
652 	ioarg[1] = size;
653 	ioctl(fd, DIOCGDELETE, ioarg);
654 	/* we don't really care if we succeed or not */
655 	return;
656 }
657 
658 /*
659  * Fill a contiguous region with all-zeroes.  Note ZEROBUFSIZE is by
660  * definition a multiple of dev_bsize.
661  */
662 void
663 blzero(int fd, ufs2_daddr_t blk, long size)
664 {
665 	static char *zero;
666 	off_t offset, len;
667 
668 	if (fd < 0)
669 		return;
670 	if (zero == NULL) {
671 		zero = calloc(ZEROBUFSIZE, 1);
672 		if (zero == NULL)
673 			errx(EEXIT, "cannot allocate buffer pool");
674 	}
675 	offset = blk * dev_bsize;
676 	if (lseek(fd, offset, 0) < 0)
677 		rwerror("SEEK BLK", blk);
678 	while (size > 0) {
679 		len = MIN(ZEROBUFSIZE, size);
680 		if (write(fd, zero, len) != len)
681 			rwerror("WRITE BLK", blk);
682 		blk += len / dev_bsize;
683 		size -= len;
684 	}
685 }
686 
687 /*
688  * Verify cylinder group's magic number and other parameters.  If the
689  * test fails, offer an option to rebuild the whole cylinder group.
690  */
691 int
692 check_cgmagic(int cg, struct bufarea *cgbp)
693 {
694 	struct cg *cgp = cgbp->b_un.b_cg;
695 
696 	/*
697 	 * Extended cylinder group checks.
698 	 */
699 	if (cg_chkmagic(cgp) &&
700 	    ((sblock.fs_magic == FS_UFS1_MAGIC &&
701 	      cgp->cg_old_niblk == sblock.fs_ipg &&
702 	      cgp->cg_ndblk <= sblock.fs_fpg &&
703 	      cgp->cg_old_ncyl <= sblock.fs_old_cpg) ||
704 	     (sblock.fs_magic == FS_UFS2_MAGIC &&
705 	      cgp->cg_niblk == sblock.fs_ipg &&
706 	      cgp->cg_ndblk <= sblock.fs_fpg &&
707 	      cgp->cg_initediblk <= sblock.fs_ipg))) {
708 		return (1);
709 	}
710 	pfatal("CYLINDER GROUP %d: BAD MAGIC NUMBER", cg);
711 	if (!reply("REBUILD CYLINDER GROUP")) {
712 		printf("YOU WILL NEED TO RERUN FSCK.\n");
713 		rerun = 1;
714 		return (1);
715 	}
716 	/*
717 	 * Zero out the cylinder group and then initialize critical fields.
718 	 * Bit maps and summaries will be recalculated by later passes.
719 	 */
720 	memset(cgp, 0, (size_t)sblock.fs_cgsize);
721 	cgp->cg_magic = CG_MAGIC;
722 	cgp->cg_cgx = cg;
723 	cgp->cg_niblk = sblock.fs_ipg;
724 	cgp->cg_initediblk = MIN(sblock.fs_ipg, 2 * INOPB(&sblock));
725 	if (cgbase(&sblock, cg) + sblock.fs_fpg < sblock.fs_size)
726 		cgp->cg_ndblk = sblock.fs_fpg;
727 	else
728 		cgp->cg_ndblk = sblock.fs_size - cgbase(&sblock, cg);
729 	cgp->cg_iusedoff = &cgp->cg_space[0] - (u_char *)(&cgp->cg_firstfield);
730 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
731 		cgp->cg_niblk = 0;
732 		cgp->cg_initediblk = 0;
733 		cgp->cg_old_ncyl = sblock.fs_old_cpg;
734 		cgp->cg_old_niblk = sblock.fs_ipg;
735 		cgp->cg_old_btotoff = cgp->cg_iusedoff;
736 		cgp->cg_old_boff = cgp->cg_old_btotoff +
737 		    sblock.fs_old_cpg * sizeof(int32_t);
738 		cgp->cg_iusedoff = cgp->cg_old_boff +
739 		    sblock.fs_old_cpg * sizeof(u_int16_t);
740 	}
741 	cgp->cg_freeoff = cgp->cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
742 	cgp->cg_nextfreeoff = cgp->cg_freeoff + howmany(sblock.fs_fpg,CHAR_BIT);
743 	if (sblock.fs_contigsumsize > 0) {
744 		cgp->cg_nclusterblks = cgp->cg_ndblk / sblock.fs_frag;
745 		cgp->cg_clustersumoff =
746 		    roundup(cgp->cg_nextfreeoff, sizeof(u_int32_t));
747 		cgp->cg_clustersumoff -= sizeof(u_int32_t);
748 		cgp->cg_clusteroff = cgp->cg_clustersumoff +
749 		    (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
750 		cgp->cg_nextfreeoff = cgp->cg_clusteroff +
751 		    howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
752 	}
753 	cgdirty(cgbp);
754 	return (0);
755 }
756 
757 /*
758  * allocate a data block with the specified number of fragments
759  */
760 ufs2_daddr_t
761 allocblk(long frags)
762 {
763 	int i, j, k, cg, baseblk;
764 	struct bufarea *cgbp;
765 	struct cg *cgp;
766 
767 	if (frags <= 0 || frags > sblock.fs_frag)
768 		return (0);
769 	for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
770 		for (j = 0; j <= sblock.fs_frag - frags; j++) {
771 			if (testbmap(i + j))
772 				continue;
773 			for (k = 1; k < frags; k++)
774 				if (testbmap(i + j + k))
775 					break;
776 			if (k < frags) {
777 				j += k;
778 				continue;
779 			}
780 			cg = dtog(&sblock, i + j);
781 			cgbp = cglookup(cg);
782 			cgp = cgbp->b_un.b_cg;
783 			if (!check_cgmagic(cg, cgbp))
784 				return (0);
785 			baseblk = dtogd(&sblock, i + j);
786 			for (k = 0; k < frags; k++) {
787 				setbmap(i + j + k);
788 				clrbit(cg_blksfree(cgp), baseblk + k);
789 			}
790 			n_blks += frags;
791 			if (frags == sblock.fs_frag)
792 				cgp->cg_cs.cs_nbfree--;
793 			else
794 				cgp->cg_cs.cs_nffree -= frags;
795 			cgdirty(cgbp);
796 			return (i + j);
797 		}
798 	}
799 	return (0);
800 }
801 
802 /*
803  * Free a previously allocated block
804  */
805 void
806 freeblk(ufs2_daddr_t blkno, long frags)
807 {
808 	struct inodesc idesc;
809 
810 	idesc.id_blkno = blkno;
811 	idesc.id_numfrags = frags;
812 	(void)pass4check(&idesc);
813 }
814 
815 /* Slow down IO so as to leave some disk bandwidth for other processes */
816 void
817 slowio_start()
818 {
819 
820 	/* Delay one in every 8 operations */
821 	slowio_pollcnt = (slowio_pollcnt + 1) & 7;
822 	if (slowio_pollcnt == 0) {
823 		gettimeofday(&slowio_starttime, NULL);
824 	}
825 }
826 
827 void
828 slowio_end()
829 {
830 	struct timeval tv;
831 	int delay_usec;
832 
833 	if (slowio_pollcnt != 0)
834 		return;
835 
836 	/* Update the slowdown interval. */
837 	gettimeofday(&tv, NULL);
838 	delay_usec = (tv.tv_sec - slowio_starttime.tv_sec) * 1000000 +
839 	    (tv.tv_usec - slowio_starttime.tv_usec);
840 	if (delay_usec < 64)
841 		delay_usec = 64;
842 	if (delay_usec > 2500000)
843 		delay_usec = 2500000;
844 	slowio_delay_usec = (slowio_delay_usec * 63 + delay_usec) >> 6;
845 	/* delay by 8 times the average IO delay */
846 	if (slowio_delay_usec > 64)
847 		usleep(slowio_delay_usec * 8);
848 }
849 
850 /*
851  * Find a pathname
852  */
853 void
854 getpathname(char *namebuf, ino_t curdir, ino_t ino)
855 {
856 	int len;
857 	char *cp;
858 	struct inodesc idesc;
859 	static int busy = 0;
860 
861 	if (curdir == ino && ino == UFS_ROOTINO) {
862 		(void)strcpy(namebuf, "/");
863 		return;
864 	}
865 	if (busy || !INO_IS_DVALID(curdir)) {
866 		(void)strcpy(namebuf, "?");
867 		return;
868 	}
869 	busy = 1;
870 	memset(&idesc, 0, sizeof(struct inodesc));
871 	idesc.id_type = DATA;
872 	idesc.id_fix = IGNORE;
873 	cp = &namebuf[MAXPATHLEN - 1];
874 	*cp = '\0';
875 	if (curdir != ino) {
876 		idesc.id_parent = curdir;
877 		goto namelookup;
878 	}
879 	while (ino != UFS_ROOTINO) {
880 		idesc.id_number = ino;
881 		idesc.id_func = findino;
882 		idesc.id_name = strdup("..");
883 		if ((ckinode(ginode(ino), &idesc) & FOUND) == 0)
884 			break;
885 	namelookup:
886 		idesc.id_number = idesc.id_parent;
887 		idesc.id_parent = ino;
888 		idesc.id_func = findname;
889 		idesc.id_name = namebuf;
890 		if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0)
891 			break;
892 		len = strlen(namebuf);
893 		cp -= len;
894 		memmove(cp, namebuf, (size_t)len);
895 		*--cp = '/';
896 		if (cp < &namebuf[UFS_MAXNAMLEN])
897 			break;
898 		ino = idesc.id_number;
899 	}
900 	busy = 0;
901 	if (ino != UFS_ROOTINO)
902 		*--cp = '?';
903 	memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
904 }
905 
906 void
907 catch(int sig __unused)
908 {
909 
910 	ckfini(0);
911 	exit(12);
912 }
913 
914 /*
915  * When preening, allow a single quit to signal
916  * a special exit after file system checks complete
917  * so that reboot sequence may be interrupted.
918  */
919 void
920 catchquit(int sig __unused)
921 {
922 	printf("returning to single-user after file system check\n");
923 	returntosingle = 1;
924 	(void)signal(SIGQUIT, SIG_DFL);
925 }
926 
927 /*
928  * determine whether an inode should be fixed.
929  */
930 int
931 dofix(struct inodesc *idesc, const char *msg)
932 {
933 
934 	switch (idesc->id_fix) {
935 
936 	case DONTKNOW:
937 		if (idesc->id_type == DATA)
938 			direrror(idesc->id_number, msg);
939 		else
940 			pwarn("%s", msg);
941 		if (preen) {
942 			printf(" (SALVAGED)\n");
943 			idesc->id_fix = FIX;
944 			return (ALTERED);
945 		}
946 		if (reply("SALVAGE") == 0) {
947 			idesc->id_fix = NOFIX;
948 			return (0);
949 		}
950 		idesc->id_fix = FIX;
951 		return (ALTERED);
952 
953 	case FIX:
954 		return (ALTERED);
955 
956 	case NOFIX:
957 	case IGNORE:
958 		return (0);
959 
960 	default:
961 		errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
962 	}
963 	/* NOTREACHED */
964 	return (0);
965 }
966 
967 #include <stdarg.h>
968 
969 /*
970  * An unexpected inconsistency occurred.
971  * Die if preening or file system is running with soft dependency protocol,
972  * otherwise just print message and continue.
973  */
974 void
975 pfatal(const char *fmt, ...)
976 {
977 	va_list ap;
978 	va_start(ap, fmt);
979 	if (!preen) {
980 		(void)vfprintf(stdout, fmt, ap);
981 		va_end(ap);
982 		if (usedsoftdep)
983 			(void)fprintf(stdout,
984 			    "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
985 		/*
986 		 * Force foreground fsck to clean up inconsistency.
987 		 */
988 		if (bkgrdflag) {
989 			cmd.value = FS_NEEDSFSCK;
990 			cmd.size = 1;
991 			if (sysctlbyname("vfs.ffs.setflags", 0, 0,
992 			    &cmd, sizeof cmd) == -1)
993 				pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
994 			fprintf(stdout, "CANNOT RUN IN BACKGROUND\n");
995 			ckfini(0);
996 			exit(EEXIT);
997 		}
998 		return;
999 	}
1000 	if (cdevname == NULL)
1001 		cdevname = strdup("fsck");
1002 	(void)fprintf(stdout, "%s: ", cdevname);
1003 	(void)vfprintf(stdout, fmt, ap);
1004 	(void)fprintf(stdout,
1005 	    "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
1006 	    cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
1007 	/*
1008 	 * Force foreground fsck to clean up inconsistency.
1009 	 */
1010 	if (bkgrdflag) {
1011 		cmd.value = FS_NEEDSFSCK;
1012 		cmd.size = 1;
1013 		if (sysctlbyname("vfs.ffs.setflags", 0, 0,
1014 		    &cmd, sizeof cmd) == -1)
1015 			pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
1016 	}
1017 	ckfini(0);
1018 	exit(EEXIT);
1019 }
1020 
1021 /*
1022  * Pwarn just prints a message when not preening or running soft dependency
1023  * protocol, or a warning (preceded by filename) when preening.
1024  */
1025 void
1026 pwarn(const char *fmt, ...)
1027 {
1028 	va_list ap;
1029 	va_start(ap, fmt);
1030 	if (preen)
1031 		(void)fprintf(stdout, "%s: ", cdevname);
1032 	(void)vfprintf(stdout, fmt, ap);
1033 	va_end(ap);
1034 }
1035 
1036 /*
1037  * Stub for routines from kernel.
1038  */
1039 void
1040 panic(const char *fmt, ...)
1041 {
1042 	va_list ap;
1043 	va_start(ap, fmt);
1044 	pfatal("INTERNAL INCONSISTENCY:");
1045 	(void)vfprintf(stdout, fmt, ap);
1046 	va_end(ap);
1047 	exit(EEXIT);
1048 }
1049