]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck_ffs/fsutil.c
zfs: merge openzfs/zfs@86783d7d9 (zfs-2.1-release) into stable/13
[FreeBSD/FreeBSD.git] / sbin / fsck_ffs / fsutil.c
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 int             sujrecovery = 0;
68
69 static struct bufarea *allocbuf(const char *);
70 static void cg_write(struct bufarea *);
71 static void slowio_start(void);
72 static void slowio_end(void);
73 static void printIOstats(void);
74
75 static long diskreads, totaldiskreads, totalreads; /* Disk cache statistics */
76 static struct timespec startpass, finishpass;
77 struct timeval slowio_starttime;
78 int slowio_delay_usec = 10000;  /* Initial IO delay for background fsck */
79 int slowio_pollcnt;
80 static struct bufarea cgblk;    /* backup buffer for cylinder group blocks */
81 static struct bufarea failedbuf; /* returned by failed getdatablk() */
82 static TAILQ_HEAD(bufqueue, bufarea) bufqueuehd; /* head of buffer cache LRU */
83 static LIST_HEAD(bufhash, bufarea) bufhashhd[HASHSIZE]; /* buffer hash list */
84 static struct bufhash freebufs; /* unused buffers */
85 static int numbufs;             /* size of buffer cache */
86 static int cachelookups;        /* number of cache lookups */
87 static int cachereads;          /* number of cache reads */
88 static int flushtries;          /* number of tries to reclaim memory */
89
90 char *buftype[BT_NUMBUFTYPES] = BT_NAMES;
91
92 void
93 fsutilinit(void)
94 {
95         diskreads = totaldiskreads = totalreads = 0;
96         bzero(&startpass, sizeof(struct timespec));
97         bzero(&finishpass, sizeof(struct timespec));
98         bzero(&slowio_starttime, sizeof(struct timeval));
99         slowio_delay_usec = 10000;
100         slowio_pollcnt = 0;
101         flushtries = 0;
102 }
103
104 int
105 ftypeok(union dinode *dp)
106 {
107         switch (DIP(dp, di_mode) & IFMT) {
108
109         case IFDIR:
110         case IFREG:
111         case IFBLK:
112         case IFCHR:
113         case IFLNK:
114         case IFSOCK:
115         case IFIFO:
116                 return (1);
117
118         default:
119                 if (debug)
120                         printf("bad file type 0%o\n", DIP(dp, di_mode));
121                 return (0);
122         }
123 }
124
125 int
126 reply(const char *question)
127 {
128         int persevere;
129         char c;
130
131         if (preen)
132                 pfatal("INTERNAL ERROR: GOT TO reply()");
133         persevere = !strcmp(question, "CONTINUE");
134         printf("\n");
135         if (!persevere && (nflag || (fswritefd < 0 && bkgrdflag == 0))) {
136                 printf("%s? no\n\n", question);
137                 resolved = 0;
138                 return (0);
139         }
140         if (yflag || (persevere && nflag)) {
141                 printf("%s? yes\n\n", question);
142                 return (1);
143         }
144         do      {
145                 printf("%s? [yn] ", question);
146                 (void) fflush(stdout);
147                 c = getc(stdin);
148                 while (c != '\n' && getc(stdin) != '\n') {
149                         if (feof(stdin)) {
150                                 resolved = 0;
151                                 return (0);
152                         }
153                 }
154         } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
155         printf("\n");
156         if (c == 'y' || c == 'Y')
157                 return (1);
158         resolved = 0;
159         return (0);
160 }
161
162 /*
163  * Look up state information for an inode.
164  */
165 struct inostat *
166 inoinfo(ino_t inum)
167 {
168         static struct inostat unallocated = { USTATE, 0, 0, 0 };
169         struct inostatlist *ilp;
170         int iloff;
171
172         if (inum >= maxino)
173                 errx(EEXIT, "inoinfo: inumber %ju out of range",
174                     (uintmax_t)inum);
175         ilp = &inostathead[inum / sblock.fs_ipg];
176         iloff = inum % sblock.fs_ipg;
177         if (iloff >= ilp->il_numalloced)
178                 return (&unallocated);
179         return (&ilp->il_stat[iloff]);
180 }
181
182 /*
183  * Malloc buffers and set up cache.
184  */
185 void
186 bufinit(void)
187 {
188         int i;
189
190         initbarea(&failedbuf, BT_UNKNOWN);
191         failedbuf.b_errs = -1;
192         failedbuf.b_un.b_buf = NULL;
193         if ((cgblk.b_un.b_buf = Malloc((unsigned int)sblock.fs_bsize)) == NULL)
194                 errx(EEXIT, "Initial malloc(%d) failed", sblock.fs_bsize);
195         initbarea(&cgblk, BT_CYLGRP);
196         numbufs = cachelookups = cachereads = 0;
197         TAILQ_INIT(&bufqueuehd);
198         LIST_INIT(&freebufs);
199         for (i = 0; i < HASHSIZE; i++)
200                 LIST_INIT(&bufhashhd[i]);
201         for (i = 0; i < BT_NUMBUFTYPES; i++) {
202                 readtime[i].tv_sec = totalreadtime[i].tv_sec = 0;
203                 readtime[i].tv_nsec = totalreadtime[i].tv_nsec = 0;
204                 readcnt[i] = totalreadcnt[i] = 0;
205         }
206 }
207
208 static struct bufarea *
209 allocbuf(const char *failreason)
210 {
211         struct bufarea *bp;
212         char *bufp;
213
214         bp = (struct bufarea *)Malloc(sizeof(struct bufarea));
215         bufp = Malloc((unsigned int)sblock.fs_bsize);
216         if (bp == NULL || bufp == NULL) {
217                 errx(EEXIT, "%s", failreason);
218                 /* NOTREACHED */
219         }
220         numbufs++;
221         bp->b_un.b_buf = bufp;
222         TAILQ_INSERT_HEAD(&bufqueuehd, bp, b_list);
223         initbarea(bp, BT_UNKNOWN);
224         return (bp);
225 }
226
227 /*
228  * Manage cylinder group buffers.
229  *
230  * Use getblk() here rather than cgget() because the cylinder group
231  * may be corrupted but we want it anyway so we can fix it.
232  */
233 static struct bufarea *cgbufs;  /* header for cylinder group cache */
234 static int flushtries;          /* number of tries to reclaim memory */
235
236 struct bufarea *
237 cglookup(int cg)
238 {
239         struct bufarea *cgbp;
240         struct cg *cgp;
241
242         if ((unsigned) cg >= sblock.fs_ncg)
243                 errx(EEXIT, "cglookup: out of range cylinder group %d", cg);
244         if (cgbufs == NULL) {
245                 cgbufs = calloc(sblock.fs_ncg, sizeof(struct bufarea));
246                 if (cgbufs == NULL)
247                         errx(EEXIT, "Cannot allocate cylinder group buffers");
248         }
249         cgbp = &cgbufs[cg];
250         if (cgbp->b_un.b_cg != NULL)
251                 return (cgbp);
252         cgp = NULL;
253         if (flushtries == 0)
254                 cgp = Malloc((unsigned int)sblock.fs_cgsize);
255         if (cgp == NULL) {
256                 if (sujrecovery)
257                         errx(EEXIT,"Ran out of memory during journal recovery");
258                 flush(fswritefd, &cgblk);
259                 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
260                 return (&cgblk);
261         }
262         cgbp->b_un.b_cg = cgp;
263         initbarea(cgbp, BT_CYLGRP);
264         getblk(cgbp, cgtod(&sblock, cg), sblock.fs_cgsize);
265         return (cgbp);
266 }
267
268 /*
269  * Mark a cylinder group buffer as dirty.
270  * Update its check-hash if they are enabled.
271  */
272 void
273 cgdirty(struct bufarea *cgbp)
274 {
275         struct cg *cg;
276
277         cg = cgbp->b_un.b_cg;
278         if ((sblock.fs_metackhash & CK_CYLGRP) != 0) {
279                 cg->cg_ckhash = 0;
280                 cg->cg_ckhash =
281                     calculate_crc32c(~0L, (void *)cg, sblock.fs_cgsize);
282         }
283         dirty(cgbp);
284 }
285
286 /*
287  * Attempt to flush a cylinder group cache entry.
288  * Return whether the flush was successful.
289  */
290 int
291 flushentry(void)
292 {
293         struct bufarea *cgbp;
294
295         if (sujrecovery || flushtries == sblock.fs_ncg || cgbufs == NULL)
296                 return (0);
297         cgbp = &cgbufs[flushtries++];
298         if (cgbp->b_un.b_cg == NULL)
299                 return (0);
300         flush(fswritefd, cgbp);
301         free(cgbp->b_un.b_buf);
302         cgbp->b_un.b_buf = NULL;
303         return (1);
304 }
305
306 /*
307  * Manage a cache of filesystem disk blocks.
308  */
309 struct bufarea *
310 getdatablk(ufs2_daddr_t blkno, long size, int type)
311 {
312         struct bufarea *bp;
313         struct bufhash *bhdp;
314
315         cachelookups++;
316         /*
317          * If out of range, return empty buffer with b_err == -1
318          *
319          * Skip check for inodes because chkrange() considers
320          * metadata areas invalid to write data.
321          */
322         if (type != BT_INODES && chkrange(blkno, size / sblock.fs_fsize)) {
323                 failedbuf.b_refcnt++;
324                 return (&failedbuf);
325         }
326         bhdp = &bufhashhd[HASH(blkno)];
327         LIST_FOREACH(bp, bhdp, b_hash)
328                 if (bp->b_bno == fsbtodb(&sblock, blkno)) {
329                         if (debug && bp->b_size != size) {
330                                 prtbuf(bp, "getdatablk: size mismatch");
331                                 pfatal("getdatablk: b_size %d != size %ld\n",
332                                     bp->b_size, size);
333                         }
334                         TAILQ_REMOVE(&bufqueuehd, bp, b_list);
335                         goto foundit;
336                 }
337         /*
338          * Move long-term busy buffer back to the front of the LRU so we 
339          * do not endless inspect them for recycling.
340          */
341         bp = TAILQ_LAST(&bufqueuehd, bufqueue);
342         if (bp != NULL && bp->b_refcnt != 0) {
343                 TAILQ_REMOVE(&bufqueuehd, bp, b_list);
344                 TAILQ_INSERT_HEAD(&bufqueuehd, bp, b_list);
345         }
346         /*
347          * Allocate up to the minimum number of buffers before
348          * considering recycling any of them.
349          */
350         if (size > sblock.fs_bsize)
351                 errx(EEXIT, "Excessive buffer size %ld > %d\n", size,
352                     sblock.fs_bsize);
353         if ((bp = LIST_FIRST(&freebufs)) != NULL) {
354                 LIST_REMOVE(bp, b_hash);
355         } else if (numbufs < MINBUFS) {
356                 bp = allocbuf("cannot create minimal buffer pool");
357         } else if (sujrecovery) {
358                 /*
359                  * SUJ recovery does not want anything written until it 
360                  * has successfully completed (so it can fail back to
361                  * full fsck). Thus, we can only recycle clean buffers.
362                  */
363                 TAILQ_FOREACH_REVERSE(bp, &bufqueuehd, bufqueue, b_list)
364                         if ((bp->b_flags & B_DIRTY) == 0 && bp->b_refcnt == 0)
365                                 break;
366                 if (bp == NULL)
367                         bp = allocbuf("Ran out of memory during "
368                             "journal recovery");
369                 else
370                         LIST_REMOVE(bp, b_hash);
371         } else {
372                 /*
373                  * Recycle oldest non-busy buffer.
374                  */
375                 TAILQ_FOREACH_REVERSE(bp, &bufqueuehd, bufqueue, b_list)
376                         if (bp->b_refcnt == 0)
377                                 break;
378                 if (bp == NULL)
379                         bp = allocbuf("Ran out of memory for buffers");
380                 else
381                         LIST_REMOVE(bp, b_hash);
382         }
383         TAILQ_REMOVE(&bufqueuehd, bp, b_list);
384         flush(fswritefd, bp);
385         bp->b_type = type;
386         LIST_INSERT_HEAD(bhdp, bp, b_hash);
387         getblk(bp, blkno, size);
388         cachereads++;
389         /* fall through */
390 foundit:
391         TAILQ_INSERT_HEAD(&bufqueuehd, bp, b_list);
392         if (debug && bp->b_type != type) {
393                 printf("getdatablk: buffer type changed to %s",
394                     BT_BUFTYPE(type));
395                 prtbuf(bp, "");
396         }
397         if (bp->b_errs == 0)
398                 bp->b_refcnt++;
399         return (bp);
400 }
401
402 void
403 getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
404 {
405         ufs2_daddr_t dblk;
406         struct timespec start, finish;
407
408         dblk = fsbtodb(&sblock, blk);
409         if (bp->b_bno == dblk) {
410                 totalreads++;
411         } else {
412                 if (debug) {
413                         readcnt[bp->b_type]++;
414                         clock_gettime(CLOCK_REALTIME_PRECISE, &start);
415                 }
416                 bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, dblk, size);
417                 if (debug) {
418                         clock_gettime(CLOCK_REALTIME_PRECISE, &finish);
419                         timespecsub(&finish, &start, &finish);
420                         timespecadd(&readtime[bp->b_type], &finish,
421                             &readtime[bp->b_type]);
422                 }
423                 bp->b_bno = dblk;
424                 bp->b_size = size;
425         }
426 }
427
428 void
429 brelse(struct bufarea *bp)
430 {
431
432         if (bp->b_refcnt <= 0)
433                 prtbuf(bp, "brelse: buffer with negative reference count");
434         bp->b_refcnt--;
435 }
436
437 void
438 binval(struct bufarea *bp)
439 {
440
441         bp->b_flags &= ~B_DIRTY;
442         LIST_REMOVE(bp, b_hash);
443         LIST_INSERT_HEAD(&freebufs, bp, b_hash);
444 }
445
446 void
447 flush(int fd, struct bufarea *bp)
448 {
449         struct inode ip;
450
451         if ((bp->b_flags & B_DIRTY) == 0)
452                 return;
453         bp->b_flags &= ~B_DIRTY;
454         if (fswritefd < 0) {
455                 pfatal("WRITING IN READ_ONLY MODE.\n");
456                 return;
457         }
458         if (bp->b_errs != 0)
459                 pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n",
460                     (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ",
461                     (long long)bp->b_bno);
462         bp->b_errs = 0;
463         /*
464          * Write using the appropriate function.
465          */
466         switch (bp->b_type) {
467         case BT_SUPERBLK:
468                 if (bp != &sblk)
469                         pfatal("BUFFER %p DOES NOT MATCH SBLK %p\n",
470                             bp, &sblk);
471                 /*
472                  * Superblocks are always pre-copied so we do not need
473                  * to check them for copy-on-write.
474                  */
475                 if (sbput(fd, bp->b_un.b_fs, 0) == 0)
476                         fsmodified = 1;
477                 break;
478         case BT_CYLGRP:
479                 /*
480                  * Cylinder groups are always pre-copied so we do not
481                  * need to check them for copy-on-write.
482                  */
483                 if (sujrecovery)
484                         cg_write(bp);
485                 if (cgput(fswritefd, &sblock, bp->b_un.b_cg) == 0)
486                         fsmodified = 1;
487                 break;
488         case BT_INODES:
489                 if (debug && sblock.fs_magic == FS_UFS2_MAGIC) {
490                         struct ufs2_dinode *dp = bp->b_un.b_dinode2;
491                         int i;
492
493                         for (i = 0; i < bp->b_size; dp++, i += sizeof(*dp)) {
494                                 if (ffs_verify_dinode_ckhash(&sblock, dp) == 0)
495                                         continue;
496                                 pwarn("flush: INODE CHECK-HASH FAILED");
497                                 ip.i_bp = bp;
498                                 ip.i_dp = (union dinode *)dp;
499                                 ip.i_number = bp->b_index + (i / sizeof(*dp));
500                                 prtinode(&ip);
501                                 if (preen || reply("FIX") != 0) {
502                                         if (preen)
503                                                 printf(" (FIXED)\n");
504                                         ffs_update_dinode_ckhash(&sblock, dp);
505                                         inodirty(&ip);
506                                 }
507                         }
508                 }
509                 /* FALLTHROUGH */
510         default:
511                 copyonwrite(&sblock, bp, std_checkblkavail);
512                 blwrite(fd, bp->b_un.b_buf, bp->b_bno, bp->b_size);
513                 break;
514         }
515 }
516
517 /*
518  * If there are any snapshots, ensure that all the blocks that they
519  * care about have been copied, then release the snapshot inodes.
520  * These operations need to be done before we rebuild the cylinder
521  * groups so that any block allocations are properly recorded.
522  * Since all the cylinder group maps have already been copied in
523  * the snapshots, no further snapshot copies will need to be done.
524  */
525 void
526 snapflush(ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t, long))
527 {
528         struct bufarea *bp;
529         int cnt;
530
531         if (snapcnt > 0) {
532                 if (debug)
533                         printf("Check for snapshot copies\n");
534                 TAILQ_FOREACH_REVERSE(bp, &bufqueuehd, bufqueue, b_list)
535                         if ((bp->b_flags & B_DIRTY) != 0)
536                                 copyonwrite(&sblock, bp, checkblkavail);
537                 for (cnt = 0; cnt < snapcnt; cnt++)
538                         irelse(&snaplist[cnt]);
539                 snapcnt = 0;
540         }
541 }
542
543 /*
544  * Journaled soft updates does not maintain cylinder group summary
545  * information during cleanup, so this routine recalculates the summary
546  * information and updates the superblock summary in preparation for
547  * writing out the cylinder group.
548  */
549 static void
550 cg_write(struct bufarea *bp)
551 {
552         ufs1_daddr_t fragno, cgbno, maxbno;
553         u_int8_t *blksfree;
554         struct csum *csp;
555         struct cg *cgp;
556         int blk;
557         int i;
558
559         /*
560          * Fix the frag and cluster summary.
561          */
562         cgp = bp->b_un.b_cg;
563         cgp->cg_cs.cs_nbfree = 0;
564         cgp->cg_cs.cs_nffree = 0;
565         bzero(&cgp->cg_frsum, sizeof(cgp->cg_frsum));
566         maxbno = fragstoblks(&sblock, sblock.fs_fpg);
567         if (sblock.fs_contigsumsize > 0) {
568                 for (i = 1; i <= sblock.fs_contigsumsize; i++)
569                         cg_clustersum(cgp)[i] = 0;
570                 bzero(cg_clustersfree(cgp), howmany(maxbno, CHAR_BIT));
571         }
572         blksfree = cg_blksfree(cgp);
573         for (cgbno = 0; cgbno < maxbno; cgbno++) {
574                 if (ffs_isfreeblock(&sblock, blksfree, cgbno))
575                         continue;
576                 if (ffs_isblock(&sblock, blksfree, cgbno)) {
577                         ffs_clusteracct(&sblock, cgp, cgbno, 1);
578                         cgp->cg_cs.cs_nbfree++;
579                         continue;
580                 }
581                 fragno = blkstofrags(&sblock, cgbno);
582                 blk = blkmap(&sblock, blksfree, fragno);
583                 ffs_fragacct(&sblock, blk, cgp->cg_frsum, 1);
584                 for (i = 0; i < sblock.fs_frag; i++)
585                         if (isset(blksfree, fragno + i))
586                                 cgp->cg_cs.cs_nffree++;
587         }
588         /*
589          * Update the superblock cg summary from our now correct values
590          * before writing the block.
591          */
592         csp = &sblock.fs_cs(&sblock, cgp->cg_cgx);
593         sblock.fs_cstotal.cs_ndir += cgp->cg_cs.cs_ndir - csp->cs_ndir;
594         sblock.fs_cstotal.cs_nbfree += cgp->cg_cs.cs_nbfree - csp->cs_nbfree;
595         sblock.fs_cstotal.cs_nifree += cgp->cg_cs.cs_nifree - csp->cs_nifree;
596         sblock.fs_cstotal.cs_nffree += cgp->cg_cs.cs_nffree - csp->cs_nffree;
597         sblock.fs_cs(&sblock, cgp->cg_cgx) = cgp->cg_cs;
598 }
599
600 void
601 rwerror(const char *mesg, ufs2_daddr_t blk)
602 {
603
604         if (bkgrdcheck)
605                 exit(EEXIT);
606         if (preen == 0)
607                 printf("\n");
608         pfatal("CANNOT %s: %ld", mesg, (long)blk);
609         if (reply("CONTINUE") == 0)
610                 exit(EEXIT);
611 }
612
613 void
614 ckfini(int markclean)
615 {
616         struct bufarea *bp, *nbp;
617         int ofsmodified, cnt, cg;
618
619         if (bkgrdflag) {
620                 unlink(snapname);
621                 if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) {
622                         cmd.value = FS_UNCLEAN;
623                         cmd.size = markclean ? -1 : 1;
624                         if (sysctlbyname("vfs.ffs.setflags", 0, 0,
625                             &cmd, sizeof cmd) == -1)
626                                 pwarn("CANNOT SET FILE SYSTEM DIRTY FLAG\n");
627                         if (!preen) {
628                                 printf("\n***** FILE SYSTEM MARKED %s *****\n",
629                                     markclean ? "CLEAN" : "DIRTY");
630                                 if (!markclean)
631                                         rerun = 1;
632                         }
633                 } else if (!preen && !markclean) {
634                         printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
635                         rerun = 1;
636                 }
637                 bkgrdflag = 0;
638         }
639         if (debug && cachelookups > 0)
640                 printf("cache with %d buffers missed %d of %d (%d%%)\n",
641                     numbufs, cachereads, cachelookups,
642                     (int)(cachereads * 100 / cachelookups));
643         if (fswritefd < 0) {
644                 (void)close(fsreadfd);
645                 return;
646         }
647
648         /*
649          * To remain idempotent with partial truncations the buffers
650          * must be flushed in this order:
651          *  1) cylinder groups (bitmaps)
652          *  2) indirect, directory, external attribute, and data blocks
653          *  3) inode blocks
654          *  4) superblock
655          * This ordering preserves access to the modified pointers
656          * until they are freed.
657          */
658         /* Step 1: cylinder groups */
659         if (debug)
660                 printf("Flush Cylinder groups\n");
661         if (cgbufs != NULL) {
662                 for (cnt = 0; cnt < sblock.fs_ncg; cnt++) {
663                         if (cgbufs[cnt].b_un.b_cg == NULL)
664                                 continue;
665                         flush(fswritefd, &cgbufs[cnt]);
666                         free(cgbufs[cnt].b_un.b_cg);
667                 }
668                 free(cgbufs);
669                 cgbufs = NULL;
670         }
671         flush(fswritefd, &cgblk);
672         free(cgblk.b_un.b_buf);
673         cgblk.b_un.b_buf = NULL;
674         cnt = 0;
675         /* Step 2: indirect, directory, external attribute, and data blocks */
676         if (debug)
677                 printf("Flush indirect, directory, external attribute, "
678                     "and data blocks\n");
679         if (pdirbp != NULL) {
680                 brelse(pdirbp);
681                 pdirbp = NULL;
682         }
683         TAILQ_FOREACH_REVERSE_SAFE(bp, &bufqueuehd, bufqueue, b_list, nbp) {
684                 switch (bp->b_type) {
685                 /* These should not be in the buffer cache list */
686                 case BT_UNKNOWN:
687                 case BT_SUPERBLK:
688                 case BT_CYLGRP:
689                 default:
690                         prtbuf(bp,"ckfini: improper buffer type on cache list");
691                         continue;
692                 /* These are the ones to flush in this step */
693                 case BT_LEVEL1:
694                 case BT_LEVEL2:
695                 case BT_LEVEL3:
696                 case BT_EXTATTR:
697                 case BT_DIRDATA:
698                 case BT_DATA:
699                         break;
700                 /* These are the ones to flush in the next step */
701                 case BT_INODES:
702                         continue;
703                 }
704                 if (debug && bp->b_refcnt != 0)
705                         prtbuf(bp, "ckfini: clearing in-use buffer");
706                 TAILQ_REMOVE(&bufqueuehd, bp, b_list);
707                 LIST_REMOVE(bp, b_hash);
708                 cnt++;
709                 flush(fswritefd, bp);
710                 free(bp->b_un.b_buf);
711                 free((char *)bp);
712         }
713         /* Step 3: inode blocks */
714         if (debug)
715                 printf("Flush inode blocks\n");
716         if (icachebp != NULL) {
717                 brelse(icachebp);
718                 icachebp = NULL;
719         }
720         TAILQ_FOREACH_REVERSE_SAFE(bp, &bufqueuehd, bufqueue, b_list, nbp) {
721                 if (debug && bp->b_refcnt != 0)
722                         prtbuf(bp, "ckfini: clearing in-use buffer");
723                 TAILQ_REMOVE(&bufqueuehd, bp, b_list);
724                 LIST_REMOVE(bp, b_hash);
725                 cnt++;
726                 flush(fswritefd, bp);
727                 free(bp->b_un.b_buf);
728                 free((char *)bp);
729         }
730         if (numbufs != cnt)
731                 errx(EEXIT, "panic: lost %d buffers", numbufs - cnt);
732         /* Step 4: superblock */
733         if (debug)
734                 printf("Flush the superblock\n");
735         flush(fswritefd, &sblk);
736         if (havesb && cursnapshot == 0 &&
737             sblk.b_bno != sblock.fs_sblockloc / dev_bsize) {
738                 if (preen || reply("UPDATE STANDARD SUPERBLOCK")) {
739                         /* Change write destination to standard superblock */
740                         sblock.fs_sblockactualloc = sblock.fs_sblockloc;
741                         sblk.b_bno = sblock.fs_sblockloc / dev_bsize;
742                         sbdirty();
743                         flush(fswritefd, &sblk);
744                 } else {
745                         markclean = 0;
746                 }
747         }
748         if (cursnapshot == 0 && sblock.fs_clean != markclean) {
749                 if ((sblock.fs_clean = markclean) != 0) {
750                         sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK);
751                         sblock.fs_pendingblocks = 0;
752                         sblock.fs_pendinginodes = 0;
753                 }
754                 sbdirty();
755                 ofsmodified = fsmodified;
756                 flush(fswritefd, &sblk);
757                 fsmodified = ofsmodified;
758                 if (!preen) {
759                         printf("\n***** FILE SYSTEM MARKED %s *****\n",
760                             markclean ? "CLEAN" : "DIRTY");
761                         if (!markclean)
762                                 rerun = 1;
763                 }
764         } else if (!preen) {
765                 if (markclean) {
766                         printf("\n***** FILE SYSTEM IS CLEAN *****\n");
767                 } else {
768                         printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
769                         rerun = 1;
770                 }
771         }
772         /*
773          * Free allocated tracking structures.
774          */
775         if (blockmap != NULL)
776                 free(blockmap);
777         blockmap = NULL;
778         if (inostathead != NULL) {
779                 for (cg = 0; cg < sblock.fs_ncg; cg++)
780                         if (inostathead[cg].il_stat != NULL)
781                                 free((char *)inostathead[cg].il_stat);
782                 free(inostathead);
783         }
784         inostathead = NULL;
785         inocleanup();
786         finalIOstats();
787         (void)close(fsreadfd);
788         (void)close(fswritefd);
789 }
790
791 /*
792  * Print out I/O statistics.
793  */
794 void
795 IOstats(char *what)
796 {
797         int i;
798
799         if (debug == 0)
800                 return;
801         if (diskreads == 0) {
802                 printf("%s: no I/O\n\n", what);
803                 return;
804         }
805         if (startpass.tv_sec == 0)
806                 startpass = startprog;
807         printf("%s: I/O statistics\n", what);
808         printIOstats();
809         totaldiskreads += diskreads;
810         diskreads = 0;
811         for (i = 0; i < BT_NUMBUFTYPES; i++) {
812                 timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
813                 totalreadcnt[i] += readcnt[i];
814                 readtime[i].tv_sec = readtime[i].tv_nsec = 0;
815                 readcnt[i] = 0;
816         }
817         clock_gettime(CLOCK_REALTIME_PRECISE, &startpass);
818 }
819
820 void
821 finalIOstats(void)
822 {
823         int i;
824
825         if (debug == 0)
826                 return;
827         printf("Final I/O statistics\n");
828         totaldiskreads += diskreads;
829         diskreads = totaldiskreads;
830         startpass = startprog;
831         for (i = 0; i < BT_NUMBUFTYPES; i++) {
832                 timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
833                 totalreadcnt[i] += readcnt[i];
834                 readtime[i] = totalreadtime[i];
835                 readcnt[i] = totalreadcnt[i];
836         }
837         printIOstats();
838 }
839
840 static void printIOstats(void)
841 {
842         long long msec, totalmsec;
843         int i;
844
845         clock_gettime(CLOCK_REALTIME_PRECISE, &finishpass);
846         timespecsub(&finishpass, &startpass, &finishpass);
847         printf("Running time: %jd.%03ld sec\n",
848                 (intmax_t)finishpass.tv_sec, finishpass.tv_nsec / 1000000);
849         printf("buffer reads by type:\n");
850         for (totalmsec = 0, i = 0; i < BT_NUMBUFTYPES; i++)
851                 totalmsec += readtime[i].tv_sec * 1000 +
852                     readtime[i].tv_nsec / 1000000;
853         if (totalmsec == 0)
854                 totalmsec = 1;
855         for (i = 0; i < BT_NUMBUFTYPES; i++) {
856                 if (readcnt[i] == 0)
857                         continue;
858                 msec =
859                     readtime[i].tv_sec * 1000 + readtime[i].tv_nsec / 1000000;
860                 printf("%21s:%8ld %2ld.%ld%% %4jd.%03ld sec %2lld.%lld%%\n",
861                     buftype[i], readcnt[i], readcnt[i] * 100 / diskreads,
862                     (readcnt[i] * 1000 / diskreads) % 10,
863                     (intmax_t)readtime[i].tv_sec, readtime[i].tv_nsec / 1000000,
864                     msec * 100 / totalmsec, (msec * 1000 / totalmsec) % 10);
865         }
866         printf("\n");
867 }
868
869 int
870 blread(int fd, char *buf, ufs2_daddr_t blk, long size)
871 {
872         char *cp;
873         int i, errs;
874         off_t offset;
875
876         offset = blk;
877         offset *= dev_bsize;
878         if (bkgrdflag)
879                 slowio_start();
880         totalreads++;
881         diskreads++;
882         if (pread(fd, buf, (int)size, offset) == size) {
883                 if (bkgrdflag)
884                         slowio_end();
885                 return (0);
886         }
887
888         /*
889          * This is handled specially here instead of in rwerror because
890          * rwerror is used for all sorts of errors, not just true read/write
891          * errors.  It should be refactored and fixed.
892          */
893         if (surrender) {
894                 pfatal("CANNOT READ_BLK: %ld", (long)blk);
895                 errx(EEXIT, "ABORTING DUE TO READ ERRORS");
896         } else
897                 rwerror("READ BLK", blk);
898
899         errs = 0;
900         memset(buf, 0, (size_t)size);
901         printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
902         for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
903                 if (pread(fd, cp, (int)secsize, offset + i) != secsize) {
904                         if (secsize != dev_bsize && dev_bsize != 1)
905                                 printf(" %jd (%jd),",
906                                     (intmax_t)(blk * dev_bsize + i) / secsize,
907                                     (intmax_t)blk + i / dev_bsize);
908                         else
909                                 printf(" %jd,", (intmax_t)blk + i / dev_bsize);
910                         errs++;
911                 }
912         }
913         printf("\n");
914         if (errs)
915                 resolved = 0;
916         return (errs);
917 }
918
919 void
920 blwrite(int fd, char *buf, ufs2_daddr_t blk, ssize_t size)
921 {
922         int i;
923         char *cp;
924         off_t offset;
925
926         if (fd < 0)
927                 return;
928         offset = blk;
929         offset *= dev_bsize;
930         if (pwrite(fd, buf, size, offset) == size) {
931                 fsmodified = 1;
932                 return;
933         }
934         resolved = 0;
935         rwerror("WRITE BLK", blk);
936         printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
937         for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
938                 if (pwrite(fd, cp, dev_bsize, offset + i) != dev_bsize)
939                         printf(" %jd,", (intmax_t)blk + i / dev_bsize);
940         printf("\n");
941         return;
942 }
943
944 void
945 blerase(int fd, ufs2_daddr_t blk, long size)
946 {
947         off_t ioarg[2];
948
949         if (fd < 0)
950                 return;
951         ioarg[0] = blk * dev_bsize;
952         ioarg[1] = size;
953         ioctl(fd, DIOCGDELETE, ioarg);
954         /* we don't really care if we succeed or not */
955         return;
956 }
957
958 /*
959  * Fill a contiguous region with all-zeroes.  Note ZEROBUFSIZE is by
960  * definition a multiple of dev_bsize.
961  */
962 void
963 blzero(int fd, ufs2_daddr_t blk, long size)
964 {
965         static char *zero;
966         off_t offset, len;
967
968         if (fd < 0)
969                 return;
970         if (zero == NULL) {
971                 zero = calloc(ZEROBUFSIZE, 1);
972                 if (zero == NULL)
973                         errx(EEXIT, "cannot allocate buffer pool");
974         }
975         offset = blk * dev_bsize;
976         if (lseek(fd, offset, 0) < 0)
977                 rwerror("SEEK BLK", blk);
978         while (size > 0) {
979                 len = MIN(ZEROBUFSIZE, size);
980                 if (write(fd, zero, len) != len)
981                         rwerror("WRITE BLK", blk);
982                 blk += len / dev_bsize;
983                 size -= len;
984         }
985 }
986
987 /*
988  * Verify cylinder group's magic number and other parameters.  If the
989  * test fails, offer an option to rebuild the whole cylinder group.
990  *
991  * Return 1 if the cylinder group is good or return 0 if it is bad.
992  */
993 #undef CHK
994 #define CHK(lhs, op, rhs, fmt)                                          \
995         if (lhs op rhs) {                                               \
996                 pwarn("UFS%d cylinder group %d failed: "                \
997                     "%s (" #fmt ") %s %s (" #fmt ")\n",                 \
998                     sblock.fs_magic == FS_UFS1_MAGIC ? 1 : 2, cg,       \
999                     #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs);     \
1000                 error = 1;                                              \
1001         }
1002 int
1003 check_cgmagic(int cg, struct bufarea *cgbp)
1004 {
1005         struct cg *cgp = cgbp->b_un.b_cg;
1006         uint32_t cghash, calchash;
1007         static int prevfailcg = -1;
1008         long start;
1009         int error;
1010
1011         /*
1012          * Extended cylinder group checks.
1013          */
1014         calchash = cgp->cg_ckhash;
1015         if ((sblock.fs_metackhash & CK_CYLGRP) != 0 &&
1016             (ckhashadd & CK_CYLGRP) == 0) {
1017                 cghash = cgp->cg_ckhash;
1018                 cgp->cg_ckhash = 0;
1019                 calchash = calculate_crc32c(~0L, (void *)cgp, sblock.fs_cgsize);
1020                 cgp->cg_ckhash = cghash;
1021         }
1022         error = 0;
1023         CHK(cgp->cg_ckhash, !=, calchash, "%jd");
1024         CHK(cg_chkmagic(cgp), ==, 0, "%jd");
1025         CHK(cgp->cg_cgx, !=, cg, "%jd");
1026         CHK(cgp->cg_ndblk, >, sblock.fs_fpg, "%jd");
1027         if (sblock.fs_magic == FS_UFS1_MAGIC) {
1028                 CHK(cgp->cg_old_niblk, !=, sblock.fs_ipg, "%jd");
1029                 CHK(cgp->cg_old_ncyl, >, sblock.fs_old_cpg, "%jd");
1030         } else if (sblock.fs_magic == FS_UFS2_MAGIC) {
1031                 CHK(cgp->cg_niblk, !=, sblock.fs_ipg, "%jd");
1032                 CHK(cgp->cg_initediblk, >, sblock.fs_ipg, "%jd");
1033         }
1034         if (cgbase(&sblock, cg) + sblock.fs_fpg < sblock.fs_size) {
1035                 CHK(cgp->cg_ndblk, !=, sblock.fs_fpg, "%jd");
1036         } else {
1037                 CHK(cgp->cg_ndblk, !=, sblock.fs_size - cgbase(&sblock, cg),
1038                     "%jd");
1039         }
1040         start = sizeof(*cgp);
1041         if (sblock.fs_magic == FS_UFS2_MAGIC) {
1042                 CHK(cgp->cg_iusedoff, !=, start, "%jd");
1043         } else if (sblock.fs_magic == FS_UFS1_MAGIC) {
1044                 CHK(cgp->cg_niblk, !=, 0, "%jd");
1045                 CHK(cgp->cg_initediblk, !=, 0, "%jd");
1046                 CHK(cgp->cg_old_ncyl, !=, sblock.fs_old_cpg, "%jd");
1047                 CHK(cgp->cg_old_niblk, !=, sblock.fs_ipg, "%jd");
1048                 CHK(cgp->cg_old_btotoff, !=, start, "%jd");
1049                 CHK(cgp->cg_old_boff, !=, cgp->cg_old_btotoff +
1050                     sblock.fs_old_cpg * sizeof(int32_t), "%jd");
1051                 CHK(cgp->cg_iusedoff, !=, cgp->cg_old_boff +
1052                     sblock.fs_old_cpg * sizeof(u_int16_t), "%jd");
1053         }
1054         CHK(cgp->cg_freeoff, !=,
1055             cgp->cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT), "%jd");
1056         if (sblock.fs_contigsumsize == 0) {
1057                 CHK(cgp->cg_nextfreeoff, !=,
1058                     cgp->cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT), "%jd");
1059         } else {
1060                 CHK(cgp->cg_nclusterblks, !=, cgp->cg_ndblk / sblock.fs_frag,
1061                     "%jd");
1062                 CHK(cgp->cg_clustersumoff, !=,
1063                     roundup(cgp->cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT),
1064                     sizeof(u_int32_t)) - sizeof(u_int32_t), "%jd");
1065                 CHK(cgp->cg_clusteroff, !=, cgp->cg_clustersumoff +
1066                     (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t), "%jd");
1067                 CHK(cgp->cg_nextfreeoff, !=, cgp->cg_clusteroff +
1068                     howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT),
1069                     "%jd");
1070         }
1071         if (error == 0)
1072                 return (1);
1073         if (prevfailcg == cg)
1074                 return (0);
1075         prevfailcg = cg;
1076         pfatal("CYLINDER GROUP %d: INTEGRITY CHECK FAILED", cg);
1077         printf("\n");
1078         return (0);
1079 }
1080
1081 void
1082 rebuild_cg(int cg, struct bufarea *cgbp)
1083 {
1084         struct cg *cgp = cgbp->b_un.b_cg;
1085         long start;
1086
1087         /*
1088          * Zero out the cylinder group and then initialize critical fields.
1089          * Bit maps and summaries will be recalculated by later passes.
1090          */
1091         memset(cgp, 0, (size_t)sblock.fs_cgsize);
1092         cgp->cg_magic = CG_MAGIC;
1093         cgp->cg_cgx = cg;
1094         cgp->cg_niblk = sblock.fs_ipg;
1095         cgp->cg_initediblk = MIN(sblock.fs_ipg, 2 * INOPB(&sblock));
1096         if (cgbase(&sblock, cg) + sblock.fs_fpg < sblock.fs_size)
1097                 cgp->cg_ndblk = sblock.fs_fpg;
1098         else
1099                 cgp->cg_ndblk = sblock.fs_size - cgbase(&sblock, cg);
1100         start = sizeof(*cgp);
1101         if (sblock.fs_magic == FS_UFS2_MAGIC) {
1102                 cgp->cg_iusedoff = start;
1103         } else if (sblock.fs_magic == FS_UFS1_MAGIC) {
1104                 cgp->cg_niblk = 0;
1105                 cgp->cg_initediblk = 0;
1106                 cgp->cg_old_ncyl = sblock.fs_old_cpg;
1107                 cgp->cg_old_niblk = sblock.fs_ipg;
1108                 cgp->cg_old_btotoff = start;
1109                 cgp->cg_old_boff = cgp->cg_old_btotoff +
1110                     sblock.fs_old_cpg * sizeof(int32_t);
1111                 cgp->cg_iusedoff = cgp->cg_old_boff +
1112                     sblock.fs_old_cpg * sizeof(u_int16_t);
1113         }
1114         cgp->cg_freeoff = cgp->cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
1115         cgp->cg_nextfreeoff = cgp->cg_freeoff + howmany(sblock.fs_fpg,CHAR_BIT);
1116         if (sblock.fs_contigsumsize > 0) {
1117                 cgp->cg_nclusterblks = cgp->cg_ndblk / sblock.fs_frag;
1118                 cgp->cg_clustersumoff =
1119                     roundup(cgp->cg_nextfreeoff, sizeof(u_int32_t));
1120                 cgp->cg_clustersumoff -= sizeof(u_int32_t);
1121                 cgp->cg_clusteroff = cgp->cg_clustersumoff +
1122                     (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
1123                 cgp->cg_nextfreeoff = cgp->cg_clusteroff +
1124                     howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
1125         }
1126         cgp->cg_ckhash = calculate_crc32c(~0L, (void *)cgp, sblock.fs_cgsize);
1127         cgdirty(cgbp);
1128 }
1129
1130 /*
1131  * allocate a data block with the specified number of fragments
1132  */
1133 ufs2_daddr_t
1134 allocblk(long startcg, long frags,
1135     ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags))
1136 {
1137         ufs2_daddr_t blkno, newblk;
1138
1139         if (sujrecovery && checkblkavail == std_checkblkavail) {
1140                 pfatal("allocblk: std_checkblkavail used for SUJ recovery\n");
1141                 return (0);
1142         }
1143         if (frags <= 0 || frags > sblock.fs_frag)
1144                 return (0);
1145         for (blkno = MAX(cgdata(&sblock, startcg), 0);
1146              blkno < maxfsblock - sblock.fs_frag;
1147              blkno += sblock.fs_frag) {
1148                 if ((newblk = (*checkblkavail)(blkno, frags)) == 0)
1149                         continue;
1150                 if (newblk > 0)
1151                         return (newblk);
1152                 if (newblk < 0)
1153                         blkno = -newblk;
1154         }
1155         for (blkno = MAX(cgdata(&sblock, 0), 0);
1156              blkno < cgbase(&sblock, startcg) - sblock.fs_frag;
1157              blkno += sblock.fs_frag) {
1158                 if ((newblk = (*checkblkavail)(blkno, frags)) == 0)
1159                         continue;
1160                 if (newblk > 0)
1161                         return (newblk);
1162                 if (newblk < 0)
1163                         blkno = -newblk;
1164         }
1165         return (0);
1166 }
1167
1168 ufs2_daddr_t
1169 std_checkblkavail(ufs2_daddr_t blkno, long frags)
1170 {
1171         struct bufarea *cgbp;
1172         struct cg *cgp;
1173         ufs2_daddr_t j, k, baseblk;
1174         long cg;
1175
1176         if ((u_int64_t)blkno > sblock.fs_size)
1177                 return (0);
1178         for (j = 0; j <= sblock.fs_frag - frags; j++) {
1179                 if (testbmap(blkno + j))
1180                         continue;
1181                 for (k = 1; k < frags; k++)
1182                         if (testbmap(blkno + j + k))
1183                                 break;
1184                 if (k < frags) {
1185                         j += k;
1186                         continue;
1187                 }
1188                 cg = dtog(&sblock, blkno + j);
1189                 cgbp = cglookup(cg);
1190                 cgp = cgbp->b_un.b_cg;
1191                 if (!check_cgmagic(cg, cgbp))
1192                         return (-((cg + 1) * sblock.fs_fpg - sblock.fs_frag));
1193                 baseblk = dtogd(&sblock, blkno + j);
1194                 for (k = 0; k < frags; k++) {
1195                         setbmap(blkno + j + k);
1196                         clrbit(cg_blksfree(cgp), baseblk + k);
1197                 }
1198                 n_blks += frags;
1199                 if (frags == sblock.fs_frag)
1200                         cgp->cg_cs.cs_nbfree--;
1201                 else
1202                         cgp->cg_cs.cs_nffree -= frags;
1203                 cgdirty(cgbp);
1204                 return (blkno + j);
1205         }
1206         return (0);
1207 }
1208
1209 /*
1210  * Check whether a file size is within the limits for the filesystem.
1211  * Return 1 when valid and 0 when too big.
1212  *
1213  * This should match the file size limit in ffs_mountfs().
1214  */
1215 int
1216 chkfilesize(mode_t mode, u_int64_t filesize)
1217 {
1218         u_int64_t kernmaxfilesize;
1219
1220         if (sblock.fs_magic == FS_UFS1_MAGIC)
1221                 kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
1222         else
1223                 kernmaxfilesize = sblock.fs_maxfilesize;
1224         if (filesize > kernmaxfilesize ||
1225             filesize > sblock.fs_maxfilesize ||
1226             (mode == IFDIR && filesize > MAXDIRSIZE)) {
1227                 if (debug)
1228                         printf("bad file size %ju:", (uintmax_t)filesize);
1229                 return (0);
1230         }
1231         return (1);
1232 }
1233
1234 /*
1235  * Slow down IO so as to leave some disk bandwidth for other processes
1236  */
1237 void
1238 slowio_start()
1239 {
1240
1241         /* Delay one in every 8 operations */
1242         slowio_pollcnt = (slowio_pollcnt + 1) & 7;
1243         if (slowio_pollcnt == 0) {
1244                 gettimeofday(&slowio_starttime, NULL);
1245         }
1246 }
1247
1248 void
1249 slowio_end()
1250 {
1251         struct timeval tv;
1252         int delay_usec;
1253
1254         if (slowio_pollcnt != 0)
1255                 return;
1256
1257         /* Update the slowdown interval. */
1258         gettimeofday(&tv, NULL);
1259         delay_usec = (tv.tv_sec - slowio_starttime.tv_sec) * 1000000 +
1260             (tv.tv_usec - slowio_starttime.tv_usec);
1261         if (delay_usec < 64)
1262                 delay_usec = 64;
1263         if (delay_usec > 2500000)
1264                 delay_usec = 2500000;
1265         slowio_delay_usec = (slowio_delay_usec * 63 + delay_usec) >> 6;
1266         /* delay by 8 times the average IO delay */
1267         if (slowio_delay_usec > 64)
1268                 usleep(slowio_delay_usec * 8);
1269 }
1270
1271 /*
1272  * Find a pathname
1273  */
1274 void
1275 getpathname(char *namebuf, ino_t curdir, ino_t ino)
1276 {
1277         int len;
1278         char *cp;
1279         struct inode ip;
1280         struct inodesc idesc;
1281         static int busy = 0;
1282
1283         if (curdir == ino && ino == UFS_ROOTINO) {
1284                 (void)strcpy(namebuf, "/");
1285                 return;
1286         }
1287         if (busy || !INO_IS_DVALID(curdir)) {
1288                 (void)strcpy(namebuf, "?");
1289                 return;
1290         }
1291         busy = 1;
1292         memset(&idesc, 0, sizeof(struct inodesc));
1293         idesc.id_type = DATA;
1294         idesc.id_fix = IGNORE;
1295         cp = &namebuf[MAXPATHLEN - 1];
1296         *cp = '\0';
1297         if (curdir != ino) {
1298                 idesc.id_parent = curdir;
1299                 goto namelookup;
1300         }
1301         while (ino != UFS_ROOTINO) {
1302                 idesc.id_number = ino;
1303                 idesc.id_func = findino;
1304                 idesc.id_name = strdup("..");
1305                 ginode(ino, &ip);
1306                 if ((ckinode(ip.i_dp, &idesc) & FOUND) == 0) {
1307                         irelse(&ip);
1308                         free(idesc.id_name);
1309                         break;
1310                 }
1311                 irelse(&ip);
1312                 free(idesc.id_name);
1313         namelookup:
1314                 idesc.id_number = idesc.id_parent;
1315                 idesc.id_parent = ino;
1316                 idesc.id_func = findname;
1317                 idesc.id_name = namebuf;
1318                 ginode(idesc.id_number, &ip);
1319                 if ((ckinode(ip.i_dp, &idesc) & FOUND) == 0) {
1320                         irelse(&ip);
1321                         break;
1322                 }
1323                 irelse(&ip);
1324                 len = strlen(namebuf);
1325                 cp -= len;
1326                 memmove(cp, namebuf, (size_t)len);
1327                 *--cp = '/';
1328                 if (cp < &namebuf[UFS_MAXNAMLEN])
1329                         break;
1330                 ino = idesc.id_number;
1331         }
1332         busy = 0;
1333         if (ino != UFS_ROOTINO)
1334                 *--cp = '?';
1335         memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
1336 }
1337
1338 void
1339 catch(int sig __unused)
1340 {
1341
1342         ckfini(0);
1343         exit(12);
1344 }
1345
1346 /*
1347  * When preening, allow a single quit to signal
1348  * a special exit after file system checks complete
1349  * so that reboot sequence may be interrupted.
1350  */
1351 void
1352 catchquit(int sig __unused)
1353 {
1354         printf("returning to single-user after file system check\n");
1355         returntosingle = 1;
1356         (void)signal(SIGQUIT, SIG_DFL);
1357 }
1358
1359 /*
1360  * determine whether an inode should be fixed.
1361  */
1362 int
1363 dofix(struct inodesc *idesc, const char *msg)
1364 {
1365
1366         switch (idesc->id_fix) {
1367
1368         case DONTKNOW:
1369                 if (idesc->id_type == DATA)
1370                         direrror(idesc->id_number, msg);
1371                 else
1372                         pwarn("%s", msg);
1373                 if (preen) {
1374                         printf(" (SALVAGED)\n");
1375                         idesc->id_fix = FIX;
1376                         return (ALTERED);
1377                 }
1378                 if (reply("SALVAGE") == 0) {
1379                         idesc->id_fix = NOFIX;
1380                         return (0);
1381                 }
1382                 idesc->id_fix = FIX;
1383                 return (ALTERED);
1384
1385         case FIX:
1386                 return (ALTERED);
1387
1388         case NOFIX:
1389         case IGNORE:
1390                 return (0);
1391
1392         default:
1393                 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
1394         }
1395         /* NOTREACHED */
1396         return (0);
1397 }
1398
1399 #include <stdarg.h>
1400
1401 /*
1402  * Print details about a buffer.
1403  */
1404 void
1405 prtbuf(struct bufarea *bp, const char *fmt, ...)
1406 {
1407         va_list ap;
1408         va_start(ap, fmt);
1409         if (preen)
1410                 (void)fprintf(stdout, "%s: ", cdevname);
1411         (void)vfprintf(stdout, fmt, ap);
1412         va_end(ap);
1413         printf(": bp %p, type %s, bno %jd, size %d, refcnt %d, flags %s, "
1414             "index %jd\n", bp, BT_BUFTYPE(bp->b_type), (intmax_t) bp->b_bno,
1415             bp->b_size, bp->b_refcnt, bp->b_flags & B_DIRTY ? "dirty" : "clean",
1416             (intmax_t) bp->b_index);
1417 }
1418
1419 /*
1420  * An unexpected inconsistency occurred.
1421  * Die if preening or file system is running with soft dependency protocol,
1422  * otherwise just print message and continue.
1423  */
1424 void
1425 pfatal(const char *fmt, ...)
1426 {
1427         va_list ap;
1428         va_start(ap, fmt);
1429         if (!preen) {
1430                 (void)vfprintf(stdout, fmt, ap);
1431                 va_end(ap);
1432                 if (usedsoftdep)
1433                         (void)fprintf(stdout,
1434                             "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
1435                 /*
1436                  * Force foreground fsck to clean up inconsistency.
1437                  */
1438                 if (bkgrdflag) {
1439                         cmd.value = FS_NEEDSFSCK;
1440                         cmd.size = 1;
1441                         if (sysctlbyname("vfs.ffs.setflags", 0, 0,
1442                             &cmd, sizeof cmd) == -1)
1443                                 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
1444                         fprintf(stdout, "CANNOT RUN IN BACKGROUND\n");
1445                         ckfini(0);
1446                         exit(EEXIT);
1447                 }
1448                 return;
1449         }
1450         if (cdevname == NULL)
1451                 cdevname = strdup("fsck");
1452         (void)fprintf(stdout, "%s: ", cdevname);
1453         (void)vfprintf(stdout, fmt, ap);
1454         (void)fprintf(stdout,
1455             "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
1456             cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
1457         /*
1458          * Force foreground fsck to clean up inconsistency.
1459          */
1460         if (bkgrdflag) {
1461                 cmd.value = FS_NEEDSFSCK;
1462                 cmd.size = 1;
1463                 if (sysctlbyname("vfs.ffs.setflags", 0, 0,
1464                     &cmd, sizeof cmd) == -1)
1465                         pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
1466         }
1467         ckfini(0);
1468         exit(EEXIT);
1469 }
1470
1471 /*
1472  * Pwarn just prints a message when not preening or running soft dependency
1473  * protocol, or a warning (preceded by filename) when preening.
1474  */
1475 void
1476 pwarn(const char *fmt, ...)
1477 {
1478         va_list ap;
1479         va_start(ap, fmt);
1480         if (preen)
1481                 (void)fprintf(stdout, "%s: ", cdevname);
1482         (void)vfprintf(stdout, fmt, ap);
1483         va_end(ap);
1484 }
1485
1486 /*
1487  * Stub for routines from kernel.
1488  */
1489 void
1490 panic(const char *fmt, ...)
1491 {
1492         va_list ap;
1493         va_start(ap, fmt);
1494         pfatal("INTERNAL INCONSISTENCY:");
1495         (void)vfprintf(stdout, fmt, ap);
1496         va_end(ap);
1497         exit(EEXIT);
1498 }