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