]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck_ffs/fsutil.c
Fix getfsstat compatibility system call panic.
[FreeBSD/FreeBSD.git] / sbin / fsck_ffs / fsutil.c
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 == NULL)
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         if (flushtries == sblock.fs_ncg || cgbufs == NULL)
258                 return (0);
259         cgbp = &cgbufs[flushtries++];
260         if (cgbp->b_un.b_cg == NULL)
261                 return (0);
262         flush(fswritefd, cgbp);
263         free(cgbp->b_un.b_buf);
264         cgbp->b_un.b_buf = NULL;
265         return (1);
266 }
267
268 /*
269  * Manage a cache of directory blocks.
270  */
271 struct bufarea *
272 getdatablk(ufs2_daddr_t blkno, long size, int type)
273 {
274         struct bufarea *bp;
275
276         TAILQ_FOREACH(bp, &bufhead, b_list)
277                 if (bp->b_bno == fsbtodb(&sblock, blkno))
278                         goto foundit;
279         TAILQ_FOREACH_REVERSE(bp, &bufhead, buflist, b_list)
280                 if ((bp->b_flags & B_INUSE) == 0)
281                         break;
282         if (bp == NULL)
283                 errx(EEXIT, "deadlocked buffer pool");
284         bp->b_type = type;
285         getblk(bp, blkno, size);
286         /* fall through */
287 foundit:
288         if (debug && bp->b_type != type)
289                 printf("Buffer type changed from %s to %s\n",
290                     buftype[bp->b_type], buftype[type]);
291         TAILQ_REMOVE(&bufhead, bp, b_list);
292         TAILQ_INSERT_HEAD(&bufhead, bp, b_list);
293         bp->b_flags |= B_INUSE;
294         return (bp);
295 }
296
297 /*
298  * Timespec operations (from <sys/time.h>).
299  */
300 #define timespecsub(vvp, uvp)                                           \
301         do {                                                            \
302                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
303                 (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
304                 if ((vvp)->tv_nsec < 0) {                               \
305                         (vvp)->tv_sec--;                                \
306                         (vvp)->tv_nsec += 1000000000;                   \
307                 }                                                       \
308         } while (0)
309 #define timespecadd(vvp, uvp)                                           \
310         do {                                                            \
311                 (vvp)->tv_sec += (uvp)->tv_sec;                         \
312                 (vvp)->tv_nsec += (uvp)->tv_nsec;                       \
313                 if ((vvp)->tv_nsec >= 1000000000) {                     \
314                         (vvp)->tv_sec++;                                \
315                         (vvp)->tv_nsec -= 1000000000;                   \
316                 }                                                       \
317         } while (0)
318
319 void
320 getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
321 {
322         ufs2_daddr_t dblk;
323         struct timespec start, finish;
324
325         dblk = fsbtodb(&sblock, blk);
326         if (bp->b_bno == dblk) {
327                 totalreads++;
328         } else {
329                 flush(fswritefd, bp);
330                 if (debug) {
331                         readcnt[bp->b_type]++;
332                         clock_gettime(CLOCK_REALTIME_PRECISE, &start);
333                 }
334                 bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, dblk, size);
335                 if (debug) {
336                         clock_gettime(CLOCK_REALTIME_PRECISE, &finish);
337                         timespecsub(&finish, &start);
338                         timespecadd(&readtime[bp->b_type], &finish);
339                 }
340                 bp->b_bno = dblk;
341                 bp->b_size = size;
342         }
343 }
344
345 void
346 flush(int fd, struct bufarea *bp)
347 {
348         int i, j;
349
350         if (!bp->b_dirty)
351                 return;
352         bp->b_dirty = 0;
353         if (fswritefd < 0) {
354                 pfatal("WRITING IN READ_ONLY MODE.\n");
355                 return;
356         }
357         if (bp->b_errs != 0)
358                 pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
359                     (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
360                     (long long)bp->b_bno);
361         bp->b_errs = 0;
362         blwrite(fd, bp->b_un.b_buf, bp->b_bno, bp->b_size);
363         if (bp != &sblk)
364                 return;
365         for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) {
366                 blwrite(fswritefd, (char *)sblock.fs_csp + i,
367                     fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag),
368                     MIN(sblock.fs_cssize - i, sblock.fs_bsize));
369         }
370 }
371
372 void
373 rwerror(const char *mesg, ufs2_daddr_t blk)
374 {
375
376         if (bkgrdcheck)
377                 exit(EEXIT);
378         if (preen == 0)
379                 printf("\n");
380         pfatal("CANNOT %s: %ld", mesg, (long)blk);
381         if (reply("CONTINUE") == 0)
382                 exit(EEXIT);
383 }
384
385 void
386 ckfini(int markclean)
387 {
388         struct bufarea *bp, *nbp;
389         int ofsmodified, cnt;
390
391         if (bkgrdflag) {
392                 unlink(snapname);
393                 if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) {
394                         cmd.value = FS_UNCLEAN;
395                         cmd.size = markclean ? -1 : 1;
396                         if (sysctlbyname("vfs.ffs.setflags", 0, 0,
397                             &cmd, sizeof cmd) == -1)
398                                 rwerror("SET FILE SYSTEM FLAGS", FS_UNCLEAN);
399                         if (!preen) {
400                                 printf("\n***** FILE SYSTEM MARKED %s *****\n",
401                                     markclean ? "CLEAN" : "DIRTY");
402                                 if (!markclean)
403                                         rerun = 1;
404                         }
405                 } else if (!preen && !markclean) {
406                         printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
407                         rerun = 1;
408                 }
409         }
410         if (debug && totalreads > 0)
411                 printf("cache with %d buffers missed %ld of %ld (%d%%)\n",
412                     numbufs, totaldiskreads, totalreads,
413                     (int)(totaldiskreads * 100 / totalreads));
414         if (fswritefd < 0) {
415                 (void)close(fsreadfd);
416                 return;
417         }
418         flush(fswritefd, &sblk);
419         if (havesb && cursnapshot == 0 && sblock.fs_magic == FS_UFS2_MAGIC &&
420             sblk.b_bno != sblock.fs_sblockloc / dev_bsize &&
421             !preen && reply("UPDATE STANDARD SUPERBLOCK")) {
422                 sblk.b_bno = sblock.fs_sblockloc / dev_bsize;
423                 sbdirty();
424                 flush(fswritefd, &sblk);
425         }
426         flush(fswritefd, &cgblk);
427         free(cgblk.b_un.b_buf);
428         cnt = 0;
429         TAILQ_FOREACH_REVERSE_SAFE(bp, &bufhead, buflist, b_list, nbp) {
430                 TAILQ_REMOVE(&bufhead, bp, b_list);
431                 cnt++;
432                 flush(fswritefd, bp);
433                 free(bp->b_un.b_buf);
434                 free((char *)bp);
435         }
436         if (numbufs != cnt)
437                 errx(EEXIT, "panic: lost %d buffers", numbufs - cnt);
438         if (cgbufs != NULL) {
439                 for (cnt = 0; cnt < sblock.fs_ncg; cnt++) {
440                         if (cgbufs[cnt].b_un.b_cg == NULL)
441                                 continue;
442                         flush(fswritefd, &cgbufs[cnt]);
443                         free(cgbufs[cnt].b_un.b_cg);
444                 }
445                 free(cgbufs);
446         }
447         pbp = pdirbp = (struct bufarea *)0;
448         if (cursnapshot == 0 && sblock.fs_clean != markclean) {
449                 if ((sblock.fs_clean = markclean) != 0) {
450                         sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK);
451                         sblock.fs_pendingblocks = 0;
452                         sblock.fs_pendinginodes = 0;
453                 }
454                 sbdirty();
455                 ofsmodified = fsmodified;
456                 flush(fswritefd, &sblk);
457                 fsmodified = ofsmodified;
458                 if (!preen) {
459                         printf("\n***** FILE SYSTEM MARKED %s *****\n",
460                             markclean ? "CLEAN" : "DIRTY");
461                         if (!markclean)
462                                 rerun = 1;
463                 }
464         } else if (!preen) {
465                 if (markclean) {
466                         printf("\n***** FILE SYSTEM IS CLEAN *****\n");
467                 } else {
468                         printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
469                         rerun = 1;
470                 }
471         }
472         (void)close(fsreadfd);
473         (void)close(fswritefd);
474 }
475
476 /*
477  * Print out I/O statistics.
478  */
479 void
480 IOstats(char *what)
481 {
482         int i;
483
484         if (debug == 0)
485                 return;
486         if (diskreads == 0) {
487                 printf("%s: no I/O\n\n", what);
488                 return;
489         }
490         if (startpass.tv_sec == 0)
491                 startpass = startprog;
492         printf("%s: I/O statistics\n", what);
493         printIOstats();
494         totaldiskreads += diskreads;
495         diskreads = 0;
496         for (i = 0; i < BT_NUMBUFTYPES; i++) {
497                 timespecadd(&totalreadtime[i], &readtime[i]);
498                 totalreadcnt[i] += readcnt[i];
499                 readtime[i].tv_sec = readtime[i].tv_nsec = 0;
500                 readcnt[i] = 0;
501         }
502         clock_gettime(CLOCK_REALTIME_PRECISE, &startpass);
503 }
504
505 void
506 finalIOstats(void)
507 {
508         int i;
509
510         if (debug == 0)
511                 return;
512         printf("Final I/O statistics\n");
513         totaldiskreads += diskreads;
514         diskreads = totaldiskreads;
515         startpass = startprog;
516         for (i = 0; i < BT_NUMBUFTYPES; i++) {
517                 timespecadd(&totalreadtime[i], &readtime[i]);
518                 totalreadcnt[i] += readcnt[i];
519                 readtime[i] = totalreadtime[i];
520                 readcnt[i] = totalreadcnt[i];
521         }
522         printIOstats();
523 }
524
525 static void printIOstats(void)
526 {
527         long long msec, totalmsec;
528         int i;
529
530         clock_gettime(CLOCK_REALTIME_PRECISE, &finishpass);
531         timespecsub(&finishpass, &startpass);
532         printf("Running time: %jd.%03ld sec\n",
533                 (intmax_t)finishpass.tv_sec, finishpass.tv_nsec / 1000000);
534         printf("buffer reads by type:\n");
535         for (totalmsec = 0, i = 0; i < BT_NUMBUFTYPES; i++)
536                 totalmsec += readtime[i].tv_sec * 1000 +
537                     readtime[i].tv_nsec / 1000000;
538         if (totalmsec == 0)
539                 totalmsec = 1;
540         for (i = 0; i < BT_NUMBUFTYPES; i++) {
541                 if (readcnt[i] == 0)
542                         continue;
543                 msec =
544                     readtime[i].tv_sec * 1000 + readtime[i].tv_nsec / 1000000;
545                 printf("%21s:%8ld %2ld.%ld%% %4jd.%03ld sec %2lld.%lld%%\n",
546                     buftype[i], readcnt[i], readcnt[i] * 100 / diskreads,
547                     (readcnt[i] * 1000 / diskreads) % 10,
548                     (intmax_t)readtime[i].tv_sec, readtime[i].tv_nsec / 1000000,
549                     msec * 100 / totalmsec, (msec * 1000 / totalmsec) % 10);
550         }
551         printf("\n");
552 }
553
554 int
555 blread(int fd, char *buf, ufs2_daddr_t blk, long size)
556 {
557         char *cp;
558         int i, errs;
559         off_t offset;
560
561         offset = blk;
562         offset *= dev_bsize;
563         if (bkgrdflag)
564                 slowio_start();
565         totalreads++;
566         diskreads++;
567         if (lseek(fd, offset, 0) < 0)
568                 rwerror("SEEK BLK", blk);
569         else if (read(fd, buf, (int)size) == size) {
570                 if (bkgrdflag)
571                         slowio_end();
572                 return (0);
573         }
574
575         /*
576          * This is handled specially here instead of in rwerror because
577          * rwerror is used for all sorts of errors, not just true read/write
578          * errors.  It should be refactored and fixed.
579          */
580         if (surrender) {
581                 pfatal("CANNOT READ_BLK: %ld", (long)blk);
582                 errx(EEXIT, "ABORTING DUE TO READ ERRORS");
583         } else
584                 rwerror("READ BLK", blk);
585
586         if (lseek(fd, offset, 0) < 0)
587                 rwerror("SEEK BLK", blk);
588         errs = 0;
589         memset(buf, 0, (size_t)size);
590         printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
591         for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
592                 if (read(fd, cp, (int)secsize) != secsize) {
593                         (void)lseek(fd, offset + i + secsize, 0);
594                         if (secsize != dev_bsize && dev_bsize != 1)
595                                 printf(" %jd (%jd),",
596                                     (intmax_t)(blk * dev_bsize + i) / secsize,
597                                     (intmax_t)blk + i / dev_bsize);
598                         else
599                                 printf(" %jd,", (intmax_t)blk + i / dev_bsize);
600                         errs++;
601                 }
602         }
603         printf("\n");
604         if (errs)
605                 resolved = 0;
606         return (errs);
607 }
608
609 void
610 blwrite(int fd, char *buf, ufs2_daddr_t blk, ssize_t size)
611 {
612         int i;
613         char *cp;
614         off_t offset;
615
616         if (fd < 0)
617                 return;
618         offset = blk;
619         offset *= dev_bsize;
620         if (lseek(fd, offset, 0) < 0)
621                 rwerror("SEEK BLK", blk);
622         else if (write(fd, buf, size) == size) {
623                 fsmodified = 1;
624                 return;
625         }
626         resolved = 0;
627         rwerror("WRITE BLK", blk);
628         if (lseek(fd, offset, 0) < 0)
629                 rwerror("SEEK BLK", blk);
630         printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
631         for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
632                 if (write(fd, cp, dev_bsize) != dev_bsize) {
633                         (void)lseek(fd, offset + i + dev_bsize, 0);
634                         printf(" %jd,", (intmax_t)blk + i / dev_bsize);
635                 }
636         printf("\n");
637         return;
638 }
639
640 void
641 blerase(int fd, ufs2_daddr_t blk, long size)
642 {
643         off_t ioarg[2];
644
645         if (fd < 0)
646                 return;
647         ioarg[0] = blk * dev_bsize;
648         ioarg[1] = size;
649         ioctl(fd, DIOCGDELETE, ioarg);
650         /* we don't really care if we succeed or not */
651         return;
652 }
653
654 /*
655  * Fill a contiguous region with all-zeroes.  Note ZEROBUFSIZE is by
656  * definition a multiple of dev_bsize.
657  */
658 void
659 blzero(int fd, ufs2_daddr_t blk, long size)
660 {
661         static char *zero;
662         off_t offset, len;
663
664         if (fd < 0)
665                 return;
666         if (zero == NULL) {
667                 zero = calloc(ZEROBUFSIZE, 1);
668                 if (zero == NULL)
669                         errx(EEXIT, "cannot allocate buffer pool");
670         }
671         offset = blk * dev_bsize;
672         if (lseek(fd, offset, 0) < 0)
673                 rwerror("SEEK BLK", blk);
674         while (size > 0) {
675                 len = MIN(ZEROBUFSIZE, size);
676                 if (write(fd, zero, len) != len)
677                         rwerror("WRITE BLK", blk);
678                 blk += len / dev_bsize;
679                 size -= len;
680         }
681 }
682
683 /*
684  * Verify cylinder group's magic number and other parameters.  If the
685  * test fails, offer an option to rebuild the whole cylinder group.
686  */
687 int
688 check_cgmagic(int cg, struct bufarea *cgbp)
689 {
690         struct cg *cgp = cgbp->b_un.b_cg;
691
692         /*
693          * Extended cylinder group checks.
694          */
695         if (cg_chkmagic(cgp) &&
696             ((sblock.fs_magic == FS_UFS1_MAGIC &&
697               cgp->cg_old_niblk == sblock.fs_ipg &&
698               cgp->cg_ndblk <= sblock.fs_fpg &&
699               cgp->cg_old_ncyl <= sblock.fs_old_cpg) ||
700              (sblock.fs_magic == FS_UFS2_MAGIC &&
701               cgp->cg_niblk == sblock.fs_ipg &&
702               cgp->cg_ndblk <= sblock.fs_fpg &&
703               cgp->cg_initediblk <= sblock.fs_ipg))) {
704                 return (1);
705         }
706         pfatal("CYLINDER GROUP %d: BAD MAGIC NUMBER", cg);
707         if (!reply("REBUILD CYLINDER GROUP")) {
708                 printf("YOU WILL NEED TO RERUN FSCK.\n");
709                 rerun = 1;
710                 return (1);
711         }
712         /*
713          * Zero out the cylinder group and then initialize critical fields.
714          * Bit maps and summaries will be recalculated by later passes.
715          */
716         memset(cgp, 0, (size_t)sblock.fs_cgsize);
717         cgp->cg_magic = CG_MAGIC;
718         cgp->cg_cgx = cg;
719         cgp->cg_niblk = sblock.fs_ipg;
720         cgp->cg_initediblk = MIN(sblock.fs_ipg, 2 * INOPB(&sblock));
721         if (cgbase(&sblock, cg) + sblock.fs_fpg < sblock.fs_size)
722                 cgp->cg_ndblk = sblock.fs_fpg;
723         else
724                 cgp->cg_ndblk = sblock.fs_size - cgbase(&sblock, cg);
725         cgp->cg_iusedoff = &cgp->cg_space[0] - (u_char *)(&cgp->cg_firstfield);
726         if (sblock.fs_magic == FS_UFS1_MAGIC) {
727                 cgp->cg_niblk = 0;
728                 cgp->cg_initediblk = 0;
729                 cgp->cg_old_ncyl = sblock.fs_old_cpg;
730                 cgp->cg_old_niblk = sblock.fs_ipg;
731                 cgp->cg_old_btotoff = cgp->cg_iusedoff;
732                 cgp->cg_old_boff = cgp->cg_old_btotoff +
733                     sblock.fs_old_cpg * sizeof(int32_t);
734                 cgp->cg_iusedoff = cgp->cg_old_boff +
735                     sblock.fs_old_cpg * sizeof(u_int16_t);
736         }
737         cgp->cg_freeoff = cgp->cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
738         cgp->cg_nextfreeoff = cgp->cg_freeoff + howmany(sblock.fs_fpg,CHAR_BIT);
739         if (sblock.fs_contigsumsize > 0) {
740                 cgp->cg_nclusterblks = cgp->cg_ndblk / sblock.fs_frag;
741                 cgp->cg_clustersumoff =
742                     roundup(cgp->cg_nextfreeoff, sizeof(u_int32_t));
743                 cgp->cg_clustersumoff -= sizeof(u_int32_t);
744                 cgp->cg_clusteroff = cgp->cg_clustersumoff +
745                     (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
746                 cgp->cg_nextfreeoff = cgp->cg_clusteroff +
747                     howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
748         }
749         dirty(cgbp);
750         return (0);
751 }
752
753 /*
754  * allocate a data block with the specified number of fragments
755  */
756 ufs2_daddr_t
757 allocblk(long frags)
758 {
759         int i, j, k, cg, baseblk;
760         struct bufarea *cgbp;
761         struct cg *cgp;
762
763         if (frags <= 0 || frags > sblock.fs_frag)
764                 return (0);
765         for (i = 0; i < maxfsblock - sblock.fs_frag; i += sblock.fs_frag) {
766                 for (j = 0; j <= sblock.fs_frag - frags; j++) {
767                         if (testbmap(i + j))
768                                 continue;
769                         for (k = 1; k < frags; k++)
770                                 if (testbmap(i + j + k))
771                                         break;
772                         if (k < frags) {
773                                 j += k;
774                                 continue;
775                         }
776                         cg = dtog(&sblock, i + j);
777                         cgbp = cgget(cg);
778                         cgp = cgbp->b_un.b_cg;
779                         if (!check_cgmagic(cg, cgbp))
780                                 return (0);
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  */
801 void
802 freeblk(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 */
812 void
813 slowio_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
823 void
824 slowio_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  */
849 void
850 getpathname(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 == 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 != 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[MAXNAMLEN])
893                         break;
894                 ino = idesc.id_number;
895         }
896         busy = 0;
897         if (ino != ROOTINO)
898                 *--cp = '?';
899         memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
900 }
901
902 void
903 catch(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  */
915 void
916 catchquit(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  */
926 int
927 dofix(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  */
970 void
971 pfatal(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  */
1021 void
1022 pwarn(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  */
1035 void
1036 panic(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 }