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