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