2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 static const char sccsid[] = "@(#)utilities.c 8.6 (Berkeley) 5/19/95";
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/sysctl.h>
43 #include <sys/disklabel.h>
44 #include <sys/ioctl.h>
47 #include <ufs/ufs/dinode.h>
48 #include <ufs/ufs/dir.h>
49 #include <ufs/ffs/fs.h>
67 static struct bufarea *allocbuf(const char *);
68 static void cg_write(struct bufarea *);
69 static void slowio_start(void);
70 static void slowio_end(void);
71 static void printIOstats(void);
73 static long diskreads, totaldiskreads, totalreads; /* Disk cache statistics */
74 static struct timespec startpass, finishpass;
75 struct timeval slowio_starttime;
76 int slowio_delay_usec = 10000; /* Initial IO delay for background fsck */
78 static struct bufarea cgblk; /* backup buffer for cylinder group blocks */
79 static struct bufarea failedbuf; /* returned by failed getdatablk() */
80 static TAILQ_HEAD(bufqueue, bufarea) bufqueuehd; /* head of buffer cache LRU */
81 static LIST_HEAD(bufhash, bufarea) bufhashhd[HASHSIZE]; /* buffer hash list */
82 static struct bufhash freebufs; /* unused buffers */
83 static int numbufs; /* size of buffer cache */
84 static int cachelookups; /* number of cache lookups */
85 static int cachereads; /* number of cache reads */
86 static int flushtries; /* number of tries to reclaim memory */
88 char *buftype[BT_NUMBUFTYPES] = BT_NAMES;
93 diskreads = totaldiskreads = totalreads = 0;
94 bzero(&startpass, sizeof(struct timespec));
95 bzero(&finishpass, sizeof(struct timespec));
96 bzero(&slowio_starttime, sizeof(struct timeval));
97 slowio_delay_usec = 10000;
103 ftypeok(union dinode *dp)
105 switch (DIP(dp, di_mode) & IFMT) {
118 printf("bad file type 0%o\n", DIP(dp, di_mode));
124 reply(const char *question)
130 pfatal("INTERNAL ERROR: GOT TO reply()");
131 persevere = strcmp(question, "CONTINUE") == 0 ||
132 strcmp(question, "LOOK FOR ALTERNATE SUPERBLOCKS") == 0;
134 if (!persevere && (nflag || (fswritefd < 0 && bkgrdflag == 0))) {
135 printf("%s? no\n\n", question);
139 if (yflag || (persevere && nflag)) {
140 printf("%s? yes\n\n", question);
144 printf("%s? [yn] ", question);
145 (void) fflush(stdout);
147 while (c != '\n' && getc(stdin) != '\n') {
153 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
155 if (c == 'y' || c == 'Y')
162 * Look up state information for an inode.
167 static struct inostat unallocated = { USTATE, 0, 0, 0 };
168 struct inostatlist *ilp;
172 errx(EEXIT, "inoinfo: inumber %ju out of range",
174 ilp = &inostathead[inum / sblock.fs_ipg];
175 iloff = inum % sblock.fs_ipg;
176 if (iloff >= ilp->il_numalloced)
177 return (&unallocated);
178 return (&ilp->il_stat[iloff]);
182 * Malloc buffers and set up cache.
189 initbarea(&failedbuf, BT_UNKNOWN);
190 failedbuf.b_errs = -1;
191 failedbuf.b_un.b_buf = NULL;
192 if ((cgblk.b_un.b_buf = Malloc((unsigned int)sblock.fs_bsize)) == NULL)
193 errx(EEXIT, "Initial malloc(%d) failed", sblock.fs_bsize);
194 initbarea(&cgblk, BT_CYLGRP);
195 numbufs = cachelookups = cachereads = 0;
196 TAILQ_INIT(&bufqueuehd);
197 LIST_INIT(&freebufs);
198 for (i = 0; i < HASHSIZE; i++)
199 LIST_INIT(&bufhashhd[i]);
200 for (i = 0; i < BT_NUMBUFTYPES; i++) {
201 readtime[i].tv_sec = totalreadtime[i].tv_sec = 0;
202 readtime[i].tv_nsec = totalreadtime[i].tv_nsec = 0;
203 readcnt[i] = totalreadcnt[i] = 0;
207 static struct bufarea *
208 allocbuf(const char *failreason)
213 bp = (struct bufarea *)Malloc(sizeof(struct bufarea));
214 bufp = Malloc((unsigned int)sblock.fs_bsize);
215 if (bp == NULL || bufp == NULL) {
216 errx(EEXIT, "%s", failreason);
220 bp->b_un.b_buf = bufp;
221 TAILQ_INSERT_HEAD(&bufqueuehd, bp, b_list);
222 initbarea(bp, BT_UNKNOWN);
227 * Manage cylinder group buffers.
229 * Use getblk() here rather than cgget() because the cylinder group
230 * may be corrupted but we want it anyway so we can fix it.
232 static struct bufarea *cgbufs; /* header for cylinder group cache */
233 static int flushtries; /* number of tries to reclaim memory */
238 struct bufarea *cgbp;
241 if ((unsigned) cg >= sblock.fs_ncg)
242 errx(EEXIT, "cglookup: out of range cylinder group %d", cg);
243 if (cgbufs == NULL) {
244 cgbufs = calloc(sblock.fs_ncg, sizeof(struct bufarea));
246 errx(EEXIT, "Cannot allocate cylinder group buffers");
249 if (cgbp->b_un.b_cg != NULL)
253 cgp = Malloc((unsigned int)sblock.fs_cgsize);
256 errx(EEXIT,"Ran out of memory during journal recovery");
257 flush(fswritefd, &cgblk);
258 getblk(&cgblk, cgtod(&sblock, cg), sblock.fs_cgsize);
261 cgbp->b_un.b_cg = cgp;
262 initbarea(cgbp, BT_CYLGRP);
263 getblk(cgbp, cgtod(&sblock, cg), sblock.fs_cgsize);
268 * Mark a cylinder group buffer as dirty.
269 * Update its check-hash if they are enabled.
272 cgdirty(struct bufarea *cgbp)
276 cg = cgbp->b_un.b_cg;
277 if ((sblock.fs_metackhash & CK_CYLGRP) != 0) {
280 calculate_crc32c(~0L, (void *)cg, sblock.fs_cgsize);
286 * Attempt to flush a cylinder group cache entry.
287 * Return whether the flush was successful.
292 struct bufarea *cgbp;
294 if (sujrecovery || flushtries == sblock.fs_ncg || cgbufs == NULL)
296 cgbp = &cgbufs[flushtries++];
297 if (cgbp->b_un.b_cg == NULL)
299 flush(fswritefd, cgbp);
300 free(cgbp->b_un.b_buf);
301 cgbp->b_un.b_buf = NULL;
306 * Manage a cache of filesystem disk blocks.
309 getdatablk(ufs2_daddr_t blkno, long size, int type)
312 struct bufhash *bhdp;
316 * If out of range, return empty buffer with b_err == -1
318 * Skip check for inodes because chkrange() considers
319 * metadata areas invalid to write data.
321 if (type != BT_INODES && chkrange(blkno, size / sblock.fs_fsize)) {
322 failedbuf.b_refcnt++;
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",
333 TAILQ_REMOVE(&bufqueuehd, bp, b_list);
337 * Move long-term busy buffer back to the front of the LRU so we
338 * do not endless inspect them for recycling.
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);
346 * Allocate up to the minimum number of buffers before
347 * considering recycling any of them.
349 if (size > sblock.fs_bsize)
350 errx(EEXIT, "Excessive buffer size %ld > %d\n", size,
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) {
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.
362 TAILQ_FOREACH_REVERSE(bp, &bufqueuehd, bufqueue, b_list)
363 if ((bp->b_flags & B_DIRTY) == 0 && bp->b_refcnt == 0)
366 bp = allocbuf("Ran out of memory during "
369 LIST_REMOVE(bp, b_hash);
372 * Recycle oldest non-busy buffer.
374 TAILQ_FOREACH_REVERSE(bp, &bufqueuehd, bufqueue, b_list)
375 if (bp->b_refcnt == 0)
378 bp = allocbuf("Ran out of memory for buffers");
380 LIST_REMOVE(bp, b_hash);
382 TAILQ_REMOVE(&bufqueuehd, bp, b_list);
383 flush(fswritefd, bp);
385 LIST_INSERT_HEAD(bhdp, bp, b_hash);
386 getblk(bp, blkno, size);
390 TAILQ_INSERT_HEAD(&bufqueuehd, bp, b_list);
391 if (debug && bp->b_type != type) {
392 printf("getdatablk: buffer type changed to %s",
402 getblk(struct bufarea *bp, ufs2_daddr_t blk, long size)
405 struct timespec start, finish;
407 dblk = fsbtodb(&sblock, blk);
408 if (bp->b_bno == dblk) {
412 readcnt[bp->b_type]++;
413 clock_gettime(CLOCK_REALTIME_PRECISE, &start);
415 bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, dblk, size);
417 clock_gettime(CLOCK_REALTIME_PRECISE, &finish);
418 timespecsub(&finish, &start, &finish);
419 timespecadd(&readtime[bp->b_type], &finish,
420 &readtime[bp->b_type]);
428 brelse(struct bufarea *bp)
431 if (bp->b_refcnt <= 0)
432 prtbuf(bp, "brelse: buffer with negative reference count");
437 binval(struct bufarea *bp)
440 bp->b_flags &= ~B_DIRTY;
441 LIST_REMOVE(bp, b_hash);
442 LIST_INSERT_HEAD(&freebufs, bp, b_hash);
446 flush(int fd, struct bufarea *bp)
450 if ((bp->b_flags & B_DIRTY) == 0)
452 bp->b_flags &= ~B_DIRTY;
454 pfatal("WRITING IN READ_ONLY MODE.\n");
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);
463 * Write using the appropriate function.
465 switch (bp->b_type) {
468 pfatal("BUFFER %p DOES NOT MATCH SBLK %p\n",
471 * Superblocks are always pre-copied so we do not need
472 * to check them for copy-on-write.
474 if (sbput(fd, bp->b_un.b_fs, 0) == 0)
479 * Cylinder groups are always pre-copied so we do not
480 * need to check them for copy-on-write.
484 if (cgput(fswritefd, &sblock, bp->b_un.b_cg) == 0)
488 if (debug && sblock.fs_magic == FS_UFS2_MAGIC) {
489 struct ufs2_dinode *dp = bp->b_un.b_dinode2;
492 for (i = 0; i < bp->b_size; dp++, i += sizeof(*dp)) {
493 if (ffs_verify_dinode_ckhash(&sblock, dp) == 0)
495 pwarn("flush: INODE CHECK-HASH FAILED");
497 ip.i_dp = (union dinode *)dp;
498 ip.i_number = bp->b_index + (i / sizeof(*dp));
500 if (preen || reply("FIX") != 0) {
502 printf(" (FIXED)\n");
503 ffs_update_dinode_ckhash(&sblock, dp);
510 copyonwrite(&sblock, bp, std_checkblkavail);
511 blwrite(fd, bp->b_un.b_buf, bp->b_bno, bp->b_size);
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.
525 snapflush(ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t, long))
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]);
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.
549 cg_write(struct bufarea *bp)
551 ufs1_daddr_t fragno, cgbno, maxbno;
559 * Fix the frag and cluster summary.
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));
571 blksfree = cg_blksfree(cgp);
572 for (cgbno = 0; cgbno < maxbno; cgbno++) {
573 if (ffs_isfreeblock(&sblock, blksfree, cgbno))
575 if (ffs_isblock(&sblock, blksfree, cgbno)) {
576 ffs_clusteracct(&sblock, cgp, cgbno, 1);
577 cgp->cg_cs.cs_nbfree++;
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++;
588 * Update the superblock cg summary from our now correct values
589 * before writing the block.
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;
600 rwerror(const char *mesg, ufs2_daddr_t blk)
607 pfatal("CANNOT %s: %ld", mesg, (long)blk);
608 if (reply("CONTINUE") == 0)
613 ckfini(int markclean)
615 struct bufarea *bp, *nbp;
616 int ofsmodified, cnt, cg;
620 if ((!(sblock.fs_flags & FS_UNCLEAN)) != markclean) {
621 cmd.value = FS_UNCLEAN;
622 cmd.size = markclean ? -1 : 1;
623 if (sysctlbyname("vfs.ffs.setflags", 0, 0,
624 &cmd, sizeof cmd) == -1)
625 pwarn("CANNOT SET FILE SYSTEM DIRTY FLAG\n");
627 printf("\n***** FILE SYSTEM MARKED %s *****\n",
628 markclean ? "CLEAN" : "DIRTY");
632 } else if (!preen && !markclean) {
633 printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
638 if (debug && cachelookups > 0)
639 printf("cache with %d buffers missed %d of %d (%d%%)\n",
640 numbufs, cachereads, cachelookups,
641 (int)(cachereads * 100 / cachelookups));
643 (void)close(fsreadfd);
648 * To remain idempotent with partial truncations the buffers
649 * must be flushed in this order:
650 * 1) cylinder groups (bitmaps)
651 * 2) indirect, directory, external attribute, and data blocks
654 * This ordering preserves access to the modified pointers
655 * until they are freed.
657 /* Step 1: cylinder groups */
659 printf("Flush Cylinder groups\n");
660 if (cgbufs != NULL) {
661 for (cnt = 0; cnt < sblock.fs_ncg; cnt++) {
662 if (cgbufs[cnt].b_un.b_cg == NULL)
664 flush(fswritefd, &cgbufs[cnt]);
665 free(cgbufs[cnt].b_un.b_cg);
670 flush(fswritefd, &cgblk);
671 free(cgblk.b_un.b_buf);
672 cgblk.b_un.b_buf = NULL;
674 /* Step 2: indirect, directory, external attribute, and data blocks */
676 printf("Flush indirect, directory, external attribute, "
677 "and data blocks\n");
678 if (pdirbp != NULL) {
682 TAILQ_FOREACH_REVERSE_SAFE(bp, &bufqueuehd, bufqueue, b_list, nbp) {
683 switch (bp->b_type) {
684 /* These should not be in the buffer cache list */
689 prtbuf(bp,"ckfini: improper buffer type on cache list");
691 /* These are the ones to flush in this step */
699 /* These are the ones to flush in the next step */
703 if (debug && bp->b_refcnt != 0)
704 prtbuf(bp, "ckfini: clearing in-use buffer");
705 TAILQ_REMOVE(&bufqueuehd, bp, b_list);
706 LIST_REMOVE(bp, b_hash);
708 flush(fswritefd, bp);
709 free(bp->b_un.b_buf);
712 /* Step 3: inode blocks */
714 printf("Flush inode blocks\n");
715 if (icachebp != NULL) {
719 TAILQ_FOREACH_REVERSE_SAFE(bp, &bufqueuehd, bufqueue, b_list, nbp) {
720 if (debug && bp->b_refcnt != 0)
721 prtbuf(bp, "ckfini: clearing in-use buffer");
722 TAILQ_REMOVE(&bufqueuehd, bp, b_list);
723 LIST_REMOVE(bp, b_hash);
725 flush(fswritefd, bp);
726 free(bp->b_un.b_buf);
730 errx(EEXIT, "panic: lost %d buffers", numbufs - cnt);
731 /* Step 4: superblock */
733 printf("Flush the superblock\n");
734 flush(fswritefd, &sblk);
735 if (havesb && cursnapshot == 0 &&
736 sblk.b_bno != sblock.fs_sblockloc / dev_bsize) {
737 if (preen || reply("UPDATE STANDARD SUPERBLOCK")) {
738 /* Change write destination to standard superblock */
739 sblock.fs_sblockactualloc = sblock.fs_sblockloc;
740 sblk.b_bno = sblock.fs_sblockloc / dev_bsize;
742 flush(fswritefd, &sblk);
747 if (cursnapshot == 0 && sblock.fs_clean != markclean) {
748 if ((sblock.fs_clean = markclean) != 0) {
749 sblock.fs_flags &= ~(FS_UNCLEAN | FS_NEEDSFSCK);
750 sblock.fs_pendingblocks = 0;
751 sblock.fs_pendinginodes = 0;
754 ofsmodified = fsmodified;
755 flush(fswritefd, &sblk);
756 fsmodified = ofsmodified;
758 printf("\n***** FILE SYSTEM MARKED %s *****\n",
759 markclean ? "CLEAN" : "DIRTY");
765 printf("\n***** FILE SYSTEM IS CLEAN *****\n");
767 printf("\n***** FILE SYSTEM STILL DIRTY *****\n");
772 * Free allocated tracking structures.
774 if (blockmap != NULL)
777 if (inostathead != NULL) {
778 for (cg = 0; cg < sblock.fs_ncg; cg++)
779 if (inostathead[cg].il_stat != NULL)
780 free((char *)inostathead[cg].il_stat);
786 (void)close(fsreadfd);
787 (void)close(fswritefd);
791 * Print out I/O statistics.
800 if (diskreads == 0) {
801 printf("%s: no I/O\n\n", what);
804 if (startpass.tv_sec == 0)
805 startpass = startprog;
806 printf("%s: I/O statistics\n", what);
808 totaldiskreads += diskreads;
810 for (i = 0; i < BT_NUMBUFTYPES; i++) {
811 timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
812 totalreadcnt[i] += readcnt[i];
813 readtime[i].tv_sec = readtime[i].tv_nsec = 0;
816 clock_gettime(CLOCK_REALTIME_PRECISE, &startpass);
826 printf("Final I/O statistics\n");
827 totaldiskreads += diskreads;
828 diskreads = totaldiskreads;
829 startpass = startprog;
830 for (i = 0; i < BT_NUMBUFTYPES; i++) {
831 timespecadd(&totalreadtime[i], &readtime[i], &totalreadtime[i]);
832 totalreadcnt[i] += readcnt[i];
833 readtime[i] = totalreadtime[i];
834 readcnt[i] = totalreadcnt[i];
839 static void printIOstats(void)
841 long long msec, totalmsec;
844 clock_gettime(CLOCK_REALTIME_PRECISE, &finishpass);
845 timespecsub(&finishpass, &startpass, &finishpass);
846 printf("Running time: %jd.%03ld sec\n",
847 (intmax_t)finishpass.tv_sec, finishpass.tv_nsec / 1000000);
848 printf("buffer reads by type:\n");
849 for (totalmsec = 0, i = 0; i < BT_NUMBUFTYPES; i++)
850 totalmsec += readtime[i].tv_sec * 1000 +
851 readtime[i].tv_nsec / 1000000;
854 for (i = 0; i < BT_NUMBUFTYPES; i++) {
858 readtime[i].tv_sec * 1000 + readtime[i].tv_nsec / 1000000;
859 printf("%21s:%8ld %2ld.%ld%% %4jd.%03ld sec %2lld.%lld%%\n",
860 buftype[i], readcnt[i], readcnt[i] * 100 / diskreads,
861 (readcnt[i] * 1000 / diskreads) % 10,
862 (intmax_t)readtime[i].tv_sec, readtime[i].tv_nsec / 1000000,
863 msec * 100 / totalmsec, (msec * 1000 / totalmsec) % 10);
869 blread(int fd, char *buf, ufs2_daddr_t blk, long size)
881 if (pread(fd, buf, (int)size, offset) == size) {
888 * This is handled specially here instead of in rwerror because
889 * rwerror is used for all sorts of errors, not just true read/write
890 * errors. It should be refactored and fixed.
893 pfatal("CANNOT READ_BLK: %ld", (long)blk);
894 errx(EEXIT, "ABORTING DUE TO READ ERRORS");
896 rwerror("READ BLK", blk);
899 memset(buf, 0, (size_t)size);
900 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:");
901 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) {
902 if (pread(fd, cp, (int)secsize, offset + i) != secsize) {
903 if (secsize != dev_bsize && dev_bsize != 1)
904 printf(" %jd (%jd),",
905 (intmax_t)(blk * dev_bsize + i) / secsize,
906 (intmax_t)blk + i / dev_bsize);
908 printf(" %jd,", (intmax_t)blk + i / dev_bsize);
919 blwrite(int fd, char *buf, ufs2_daddr_t blk, ssize_t size)
929 if (pwrite(fd, buf, size, offset) == size) {
934 rwerror("WRITE BLK", blk);
935 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:");
936 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize)
937 if (pwrite(fd, cp, dev_bsize, offset + i) != dev_bsize)
938 printf(" %jd,", (intmax_t)blk + i / dev_bsize);
944 blerase(int fd, ufs2_daddr_t blk, long size)
950 ioarg[0] = blk * dev_bsize;
952 ioctl(fd, DIOCGDELETE, ioarg);
953 /* we don't really care if we succeed or not */
958 * Fill a contiguous region with all-zeroes. Note ZEROBUFSIZE is by
959 * definition a multiple of dev_bsize.
962 blzero(int fd, ufs2_daddr_t blk, long size)
970 zero = calloc(ZEROBUFSIZE, 1);
972 errx(EEXIT, "cannot allocate buffer pool");
974 offset = blk * dev_bsize;
975 if (lseek(fd, offset, 0) < 0)
976 rwerror("SEEK BLK", blk);
978 len = MIN(ZEROBUFSIZE, size);
979 if (write(fd, zero, len) != len)
980 rwerror("WRITE BLK", blk);
981 blk += len / dev_bsize;
987 * Verify cylinder group's magic number and other parameters. If the
988 * test fails, offer an option to rebuild the whole cylinder group.
990 * Return 1 if the cylinder group is good or return 0 if it is bad.
993 #define CHK(lhs, op, rhs, fmt) \
995 pwarn("UFS%d cylinder group %d failed: " \
996 "%s (" #fmt ") %s %s (" #fmt ")\n", \
997 sblock.fs_magic == FS_UFS1_MAGIC ? 1 : 2, cg, \
998 #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs); \
1002 check_cgmagic(int cg, struct bufarea *cgbp)
1004 struct cg *cgp = cgbp->b_un.b_cg;
1005 uint32_t cghash, calchash;
1006 static int prevfailcg = -1;
1011 * Extended cylinder group checks.
1013 calchash = cgp->cg_ckhash;
1014 if ((sblock.fs_metackhash & CK_CYLGRP) != 0 &&
1015 (ckhashadd & CK_CYLGRP) == 0) {
1016 cghash = cgp->cg_ckhash;
1018 calchash = calculate_crc32c(~0L, (void *)cgp, sblock.fs_cgsize);
1019 cgp->cg_ckhash = cghash;
1022 CHK(cgp->cg_ckhash, !=, calchash, "%jd");
1023 CHK(cg_chkmagic(cgp), ==, 0, "%jd");
1024 CHK(cgp->cg_cgx, !=, cg, "%jd");
1025 CHK(cgp->cg_ndblk, >, sblock.fs_fpg, "%jd");
1026 if (sblock.fs_magic == FS_UFS1_MAGIC) {
1027 CHK(cgp->cg_old_niblk, !=, sblock.fs_ipg, "%jd");
1028 CHK(cgp->cg_old_ncyl, >, sblock.fs_old_cpg, "%jd");
1029 } else if (sblock.fs_magic == FS_UFS2_MAGIC) {
1030 CHK(cgp->cg_niblk, !=, sblock.fs_ipg, "%jd");
1031 CHK(cgp->cg_initediblk, >, sblock.fs_ipg, "%jd");
1033 if (cgbase(&sblock, cg) + sblock.fs_fpg < sblock.fs_size) {
1034 CHK(cgp->cg_ndblk, !=, sblock.fs_fpg, "%jd");
1036 CHK(cgp->cg_ndblk, !=, sblock.fs_size - cgbase(&sblock, cg),
1039 start = sizeof(*cgp);
1040 if (sblock.fs_magic == FS_UFS2_MAGIC) {
1041 CHK(cgp->cg_iusedoff, !=, start, "%jd");
1042 } else if (sblock.fs_magic == FS_UFS1_MAGIC) {
1043 CHK(cgp->cg_niblk, !=, 0, "%jd");
1044 CHK(cgp->cg_initediblk, !=, 0, "%jd");
1045 CHK(cgp->cg_old_ncyl, !=, sblock.fs_old_cpg, "%jd");
1046 CHK(cgp->cg_old_niblk, !=, sblock.fs_ipg, "%jd");
1047 CHK(cgp->cg_old_btotoff, !=, start, "%jd");
1048 CHK(cgp->cg_old_boff, !=, cgp->cg_old_btotoff +
1049 sblock.fs_old_cpg * sizeof(int32_t), "%jd");
1050 CHK(cgp->cg_iusedoff, !=, cgp->cg_old_boff +
1051 sblock.fs_old_cpg * sizeof(u_int16_t), "%jd");
1053 CHK(cgp->cg_freeoff, !=,
1054 cgp->cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT), "%jd");
1055 if (sblock.fs_contigsumsize == 0) {
1056 CHK(cgp->cg_nextfreeoff, !=,
1057 cgp->cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT), "%jd");
1059 CHK(cgp->cg_nclusterblks, !=, cgp->cg_ndblk / sblock.fs_frag,
1061 CHK(cgp->cg_clustersumoff, !=,
1062 roundup(cgp->cg_freeoff + howmany(sblock.fs_fpg, CHAR_BIT),
1063 sizeof(u_int32_t)) - sizeof(u_int32_t), "%jd");
1064 CHK(cgp->cg_clusteroff, !=, cgp->cg_clustersumoff +
1065 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t), "%jd");
1066 CHK(cgp->cg_nextfreeoff, !=, cgp->cg_clusteroff +
1067 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT),
1072 if (prevfailcg == cg)
1075 pfatal("CYLINDER GROUP %d: INTEGRITY CHECK FAILED", cg);
1081 rebuild_cg(int cg, struct bufarea *cgbp)
1083 struct cg *cgp = cgbp->b_un.b_cg;
1087 * Zero out the cylinder group and then initialize critical fields.
1088 * Bit maps and summaries will be recalculated by later passes.
1090 memset(cgp, 0, (size_t)sblock.fs_cgsize);
1091 cgp->cg_magic = CG_MAGIC;
1093 cgp->cg_niblk = sblock.fs_ipg;
1094 cgp->cg_initediblk = MIN(sblock.fs_ipg, 2 * INOPB(&sblock));
1095 if (cgbase(&sblock, cg) + sblock.fs_fpg < sblock.fs_size)
1096 cgp->cg_ndblk = sblock.fs_fpg;
1098 cgp->cg_ndblk = sblock.fs_size - cgbase(&sblock, cg);
1099 start = sizeof(*cgp);
1100 if (sblock.fs_magic == FS_UFS2_MAGIC) {
1101 cgp->cg_iusedoff = start;
1102 } else if (sblock.fs_magic == FS_UFS1_MAGIC) {
1104 cgp->cg_initediblk = 0;
1105 cgp->cg_old_ncyl = sblock.fs_old_cpg;
1106 cgp->cg_old_niblk = sblock.fs_ipg;
1107 cgp->cg_old_btotoff = start;
1108 cgp->cg_old_boff = cgp->cg_old_btotoff +
1109 sblock.fs_old_cpg * sizeof(int32_t);
1110 cgp->cg_iusedoff = cgp->cg_old_boff +
1111 sblock.fs_old_cpg * sizeof(u_int16_t);
1113 cgp->cg_freeoff = cgp->cg_iusedoff + howmany(sblock.fs_ipg, CHAR_BIT);
1114 cgp->cg_nextfreeoff = cgp->cg_freeoff + howmany(sblock.fs_fpg,CHAR_BIT);
1115 if (sblock.fs_contigsumsize > 0) {
1116 cgp->cg_nclusterblks = cgp->cg_ndblk / sblock.fs_frag;
1117 cgp->cg_clustersumoff =
1118 roundup(cgp->cg_nextfreeoff, sizeof(u_int32_t));
1119 cgp->cg_clustersumoff -= sizeof(u_int32_t);
1120 cgp->cg_clusteroff = cgp->cg_clustersumoff +
1121 (sblock.fs_contigsumsize + 1) * sizeof(u_int32_t);
1122 cgp->cg_nextfreeoff = cgp->cg_clusteroff +
1123 howmany(fragstoblks(&sblock, sblock.fs_fpg), CHAR_BIT);
1125 cgp->cg_ckhash = calculate_crc32c(~0L, (void *)cgp, sblock.fs_cgsize);
1130 * allocate a data block with the specified number of fragments
1133 allocblk(long startcg, long frags,
1134 ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags))
1136 ufs2_daddr_t blkno, newblk;
1138 if (sujrecovery && checkblkavail == std_checkblkavail) {
1139 pfatal("allocblk: std_checkblkavail used for SUJ recovery\n");
1142 if (frags <= 0 || frags > sblock.fs_frag)
1144 for (blkno = MAX(cgdata(&sblock, startcg), 0);
1145 blkno < maxfsblock - sblock.fs_frag;
1146 blkno += sblock.fs_frag) {
1147 if ((newblk = (*checkblkavail)(blkno, frags)) == 0)
1154 for (blkno = MAX(cgdata(&sblock, 0), 0);
1155 blkno < cgbase(&sblock, startcg) - sblock.fs_frag;
1156 blkno += sblock.fs_frag) {
1157 if ((newblk = (*checkblkavail)(blkno, frags)) == 0)
1168 std_checkblkavail(ufs2_daddr_t blkno, long frags)
1170 struct bufarea *cgbp;
1172 ufs2_daddr_t j, k, baseblk;
1175 if ((u_int64_t)blkno > sblock.fs_size)
1177 for (j = 0; j <= sblock.fs_frag - frags; j++) {
1178 if (testbmap(blkno + j))
1180 for (k = 1; k < frags; k++)
1181 if (testbmap(blkno + j + k))
1187 cg = dtog(&sblock, blkno + j);
1188 cgbp = cglookup(cg);
1189 cgp = cgbp->b_un.b_cg;
1190 if (!check_cgmagic(cg, cgbp))
1191 return (-((cg + 1) * sblock.fs_fpg - sblock.fs_frag));
1192 baseblk = dtogd(&sblock, blkno + j);
1193 for (k = 0; k < frags; k++) {
1194 setbmap(blkno + j + k);
1195 clrbit(cg_blksfree(cgp), baseblk + k);
1198 if (frags == sblock.fs_frag)
1199 cgp->cg_cs.cs_nbfree--;
1201 cgp->cg_cs.cs_nffree -= frags;
1209 * Check whether a file size is within the limits for the filesystem.
1210 * Return 1 when valid and 0 when too big.
1212 * This should match the file size limit in ffs_mountfs().
1215 chkfilesize(mode_t mode, u_int64_t filesize)
1217 u_int64_t kernmaxfilesize;
1219 if (sblock.fs_magic == FS_UFS1_MAGIC)
1220 kernmaxfilesize = (off_t)0x40000000 * sblock.fs_bsize - 1;
1222 kernmaxfilesize = sblock.fs_maxfilesize;
1223 if (filesize > kernmaxfilesize ||
1224 filesize > sblock.fs_maxfilesize ||
1225 (mode == IFDIR && filesize > MAXDIRSIZE)) {
1227 printf("bad file size %ju:", (uintmax_t)filesize);
1234 * Slow down IO so as to leave some disk bandwidth for other processes
1240 /* Delay one in every 8 operations */
1241 slowio_pollcnt = (slowio_pollcnt + 1) & 7;
1242 if (slowio_pollcnt == 0) {
1243 gettimeofday(&slowio_starttime, NULL);
1253 if (slowio_pollcnt != 0)
1256 /* Update the slowdown interval. */
1257 gettimeofday(&tv, NULL);
1258 delay_usec = (tv.tv_sec - slowio_starttime.tv_sec) * 1000000 +
1259 (tv.tv_usec - slowio_starttime.tv_usec);
1260 if (delay_usec < 64)
1262 if (delay_usec > 2500000)
1263 delay_usec = 2500000;
1264 slowio_delay_usec = (slowio_delay_usec * 63 + delay_usec) >> 6;
1265 /* delay by 8 times the average IO delay */
1266 if (slowio_delay_usec > 64)
1267 usleep(slowio_delay_usec * 8);
1274 getpathname(char *namebuf, ino_t curdir, ino_t ino)
1279 struct inodesc idesc;
1280 static int busy = 0;
1282 if (curdir == ino && ino == UFS_ROOTINO) {
1283 (void)strcpy(namebuf, "/");
1286 if (busy || !INO_IS_DVALID(curdir)) {
1287 (void)strcpy(namebuf, "?");
1291 memset(&idesc, 0, sizeof(struct inodesc));
1292 idesc.id_type = DATA;
1293 idesc.id_fix = IGNORE;
1294 cp = &namebuf[MAXPATHLEN - 1];
1296 if (curdir != ino) {
1297 idesc.id_parent = curdir;
1300 while (ino != UFS_ROOTINO) {
1301 idesc.id_number = ino;
1302 idesc.id_func = findino;
1303 idesc.id_name = strdup("..");
1305 if ((ckinode(ip.i_dp, &idesc) & FOUND) == 0) {
1307 free(idesc.id_name);
1311 free(idesc.id_name);
1313 idesc.id_number = idesc.id_parent;
1314 idesc.id_parent = ino;
1315 idesc.id_func = findname;
1316 idesc.id_name = namebuf;
1317 ginode(idesc.id_number, &ip);
1318 if ((ckinode(ip.i_dp, &idesc) & FOUND) == 0) {
1323 len = strlen(namebuf);
1325 memmove(cp, namebuf, (size_t)len);
1327 if (cp < &namebuf[UFS_MAXNAMLEN])
1329 ino = idesc.id_number;
1332 if (ino != UFS_ROOTINO)
1334 memmove(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp));
1338 catch(int sig __unused)
1346 * When preening, allow a single quit to signal
1347 * a special exit after file system checks complete
1348 * so that reboot sequence may be interrupted.
1351 catchquit(int sig __unused)
1353 printf("returning to single-user after file system check\n");
1355 (void)signal(SIGQUIT, SIG_DFL);
1359 * determine whether an inode should be fixed.
1362 dofix(struct inodesc *idesc, const char *msg)
1365 switch (idesc->id_fix) {
1368 if (idesc->id_type == DATA)
1369 direrror(idesc->id_number, msg);
1373 printf(" (SALVAGED)\n");
1374 idesc->id_fix = FIX;
1377 if (reply("SALVAGE") == 0) {
1378 idesc->id_fix = NOFIX;
1381 idesc->id_fix = FIX;
1392 errx(EEXIT, "UNKNOWN INODESC FIX MODE %d", idesc->id_fix);
1401 * Print details about a buffer.
1404 prtbuf(struct bufarea *bp, const char *fmt, ...)
1409 (void)fprintf(stdout, "%s: ", cdevname);
1410 (void)vfprintf(stdout, fmt, ap);
1412 printf(": bp %p, type %s, bno %jd, size %d, refcnt %d, flags %s, "
1413 "index %jd\n", bp, BT_BUFTYPE(bp->b_type), (intmax_t) bp->b_bno,
1414 bp->b_size, bp->b_refcnt, bp->b_flags & B_DIRTY ? "dirty" : "clean",
1415 (intmax_t) bp->b_index);
1419 * An unexpected inconsistency occurred.
1420 * Die if preening or file system is running with soft dependency protocol,
1421 * otherwise just print message and continue.
1424 pfatal(const char *fmt, ...)
1429 (void)vfprintf(stdout, fmt, ap);
1432 (void)fprintf(stdout,
1433 "\nUNEXPECTED SOFT UPDATE INCONSISTENCY\n");
1435 * Force foreground fsck to clean up inconsistency.
1438 cmd.value = FS_NEEDSFSCK;
1440 if (sysctlbyname("vfs.ffs.setflags", 0, 0,
1441 &cmd, sizeof cmd) == -1)
1442 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
1443 fprintf(stdout, "CANNOT RUN IN BACKGROUND\n");
1449 if (cdevname == NULL)
1450 cdevname = strdup("fsck");
1451 (void)fprintf(stdout, "%s: ", cdevname);
1452 (void)vfprintf(stdout, fmt, ap);
1453 (void)fprintf(stdout,
1454 "\n%s: UNEXPECTED%sINCONSISTENCY; RUN fsck MANUALLY.\n",
1455 cdevname, usedsoftdep ? " SOFT UPDATE " : " ");
1457 * Force foreground fsck to clean up inconsistency.
1460 cmd.value = FS_NEEDSFSCK;
1462 if (sysctlbyname("vfs.ffs.setflags", 0, 0,
1463 &cmd, sizeof cmd) == -1)
1464 pwarn("CANNOT SET FS_NEEDSFSCK FLAG\n");
1471 * Pwarn just prints a message when not preening or running soft dependency
1472 * protocol, or a warning (preceded by filename) when preening.
1475 pwarn(const char *fmt, ...)
1480 (void)fprintf(stdout, "%s: ", cdevname);
1481 (void)vfprintf(stdout, fmt, ap);
1486 * Stub for routines from kernel.
1489 panic(const char *fmt, ...)
1493 pfatal("INTERNAL INCONSISTENCY:");
1494 (void)vfprintf(stdout, fmt, ap);